From 8ce09a0057189733c814b01d0ecfa7ec405014a0 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 15 May 2019 19:45:50 +0530 Subject: [PATCH] VMware: fix inventory plugin (#56431) * Fixed regression introduced in #56071 * Added test to check if hostvars are populated * Fix toml installation logic Fixes: #56413 Signed-off-by: Abhijeet Kasurde --- .../plugins/inventory/vmware_vm_inventory.py | 2 +- .../inventory_vmware_vm_inventory/runme.sh | 45 ++++++++++++------- .../test_vmware_vm_inventory.yml | 6 +++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/ansible/plugins/inventory/vmware_vm_inventory.py b/lib/ansible/plugins/inventory/vmware_vm_inventory.py index a6e0bb34e1..9b44028874 100644 --- a/lib/ansible/plugins/inventory/vmware_vm_inventory.py +++ b/lib/ansible/plugins/inventory/vmware_vm_inventory.py @@ -448,7 +448,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable): 'customValue', ] field_mgr = [] - if self.content.customFieldsManager: + if self.pyv.content.customFieldsManager: field_mgr = self.pyv.content.customFieldsManager.field for vm_prop in vm_properties: diff --git a/test/integration/targets/inventory_vmware_vm_inventory/runme.sh b/test/integration/targets/inventory_vmware_vm_inventory/runme.sh index 7ba306f700..6385b5ea10 100755 --- a/test/integration/targets/inventory_vmware_vm_inventory/runme.sh +++ b/test/integration/targets/inventory_vmware_vm_inventory/runme.sh @@ -11,7 +11,9 @@ export ANSIBLE_CONFIG=ansible.cfg export VMWARE_SERVER="${VCENTER_HOST}" export VMWARE_USERNAME="${VMWARE_USERNAME:-user}" export VMWARE_PASSWORD="${VMWARE_PASSWORD:-pass}" +port=5000 VMWARE_CONFIG=test-config.vmware.yaml +inventory_cache="$(pwd)/inventory_cache" cat > "$VMWARE_CONFIG" < /dev/null 2>&1 +curl "http://${VCENTER_HOST}:${port}/killall" > /dev/null 2>&1 echo "Start new VCSIM server" -curl "http://${VCENTER_HOST}:5000/spawn?datacenter=1&cluster=1&folder=0" > /dev/null 2>&1 +curl "http://${VCENTER_HOST}:${port}/spawn?datacenter=1&cluster=1&folder=0" > /dev/null 2>&1 echo "Debugging new instances" -curl "http://${VCENTER_HOST}:5000/govc_find" +curl "http://${VCENTER_HOST}:${port}/govc_find" # Get inventory ansible-inventory -i ${VMWARE_CONFIG} --list -# Get inventory using YAML -ansible-inventory -i ${VMWARE_CONFIG} --list --yaml - -# Install TOML for --toml -${PYTHON} -m pip install toml - -# Get inventory using TOML -ansible-inventory -i ${VMWARE_CONFIG} --list --toml - echo "Check if cache is working for inventory plugin" -ls "$(pwd)/inventory_cache/vmware_vm_*" > /dev/null 2>&1 -if [ $? -ne 0 ]; then +if [ ! -n "$(find "${inventory_cache}" -maxdepth 1 -name 'vmware_vm_inventory_*' -print -quit)" ]; then echo "Cache directory not found. Please debug" exit 1 fi echo "Cache is working" +# Get inventory using YAML +ansible-inventory -i ${VMWARE_CONFIG} --list --yaml + +# Install TOML for --toml +${PYTHON} -m pip freeze | grep toml > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "Installing TOML package" + ${PYTHON} -m pip install toml +else + echo "TOML package already exists, skipping installation" +fi + +# Get inventory using TOML +ansible-inventory -i ${VMWARE_CONFIG} --list --toml +if [ $? -ne 0 ]; then + echo "Inventory plugin failed to list inventory host using --toml, please debug" + exit 1 +fi + # Test playbook with given inventory ansible-playbook -i ${VMWARE_CONFIG} test_vmware_vm_inventory.yml --connection=local "$@" diff --git a/test/integration/targets/inventory_vmware_vm_inventory/test_vmware_vm_inventory.yml b/test/integration/targets/inventory_vmware_vm_inventory/test_vmware_vm_inventory.yml index f59200aaa7..cb75ff1f38 100644 --- a/test/integration/targets/inventory_vmware_vm_inventory/test_vmware_vm_inventory.yml +++ b/test/integration/targets/inventory_vmware_vm_inventory/test_vmware_vm_inventory.yml @@ -16,3 +16,9 @@ with_items: - all - otherGuest + + - name: Check if Hostname and other details are populated in hostvars + assert: + that: + - hostvars[item].name is defined + with_items: "{{ groups['all'] }}"