From 5962337332c47ff19e7809259b899e89a7d8dcf2 Mon Sep 17 00:00:00 2001 From: Tim Rightnour Date: Sun, 19 Nov 2017 07:52:38 -0700 Subject: [PATCH] Add additional facts to the fact gathering of vmware (#31632) Adds facts about datastores, esxi_host, vm files, ha state, question, is_template, consolidation status and hw_files. Also, adds integration tests for vmware fact enhancement. --- lib/ansible/module_utils/vmware.py | 49 +++++++++++++++++++ .../targets/vmware_guest_facts/tasks/main.yml | 10 ++++ 2 files changed, 59 insertions(+) diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index b3451e37e7b..1aa1d1a8910 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -277,8 +277,16 @@ def gather_vm_facts(content, vm): 'hw_cores_per_socket': vm.config.hardware.numCoresPerSocket, 'hw_memtotal_mb': vm.config.hardware.memoryMB, 'hw_interfaces': [], + 'hw_datastores': [], + 'hw_files': [], + 'hw_esxi_host': None, + 'hw_guest_ha_state': vm.summary.runtime.dasVmProtection, + 'hw_is_template': vm.config.template, + 'hw_folder': None, 'guest_tools_status': _get_vm_prop(vm, ('guest', 'toolsRunningStatus')), 'guest_tools_version': _get_vm_prop(vm, ('guest', 'toolsVersion')), + 'guest_question': vm.summary.runtime.question, + 'guest_consolidation_needed': vm.summary.runtime.consolidationNeeded, 'ipv4': None, 'ipv6': None, 'annotation': vm.config.annotation, @@ -287,6 +295,47 @@ def gather_vm_facts(content, vm): 'current_snapshot': None, } + # facts that may or may not exist + if vm.summary.runtime.host: + host = vm.summary.runtime.host + facts['hw_esxi_host'] = host.summary.config.name + + datastores = vm.datastore + for ds in datastores: + facts['hw_datastores'].append(ds.info.name) + + try: + files = vm.config.files + layout = vm.layout + if files: + facts['hw_files'] = [files.vmPathName] + for item in layout.snapshot: + for snap in item.snapshotFile: + facts['hw_files'].append(files.snapshotDirectory + snap) + for item in layout.configFile: + facts['hw_files'].append(os.path.dirname(files.vmPathName) + '/' + item) + for item in vm.layout.logFile: + facts['hw_files'].append(files.logDirectory + item) + for item in vm.layout.disk: + for disk in item.diskFile: + facts['hw_files'].append(disk) + except: + pass + + folder = vm.parent + if folder: + foldername = folder.name + fp = folder.parent + # climb back up the tree to find our path, stop before the root folder + while fp is not None and fp.name is not None and fp != content.rootFolder: + foldername = fp.name + '/' + foldername + try: + fp = fp.parent + except: + break + foldername = '/' + foldername + facts['hw_folder'] = foldername + cfm = content.customFieldsManager # Resolve custom values for value_obj in vm.summary.customValue: diff --git a/test/integration/targets/vmware_guest_facts/tasks/main.yml b/test/integration/targets/vmware_guest_facts/tasks/main.yml index 3edcf7dc082..37540b56ab8 100644 --- a/test/integration/targets/vmware_guest_facts/tasks/main.yml +++ b/test/integration/targets/vmware_guest_facts/tasks/main.yml @@ -42,6 +42,13 @@ - set_fact: dc1="{{ datacenters['json'][0] }}" +- name: get a list of hosts from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=H + register: hosts + +- set_fact: h1="{{ hosts['json'][0] }}" + - name: get a list of virtual machines from vcsim uri: url: http://{{ vcsim }}:5000/govc_find?filter=VM @@ -68,6 +75,8 @@ - "guest_facts_0001['instance']['hw_name'] == vm1 | basename" - "guest_facts_0001['instance']['hw_product_uuid'] is defined" - "guest_facts_0001['instance']['hw_cores_per_socket'] is defined" + - "guest_facts_0001['instance']['hw_datastores'] is defined" + - "guest_facts_0001['instance']['hw_esxi_host'] == h1 | basename" - set_fact: vm1_uuid="{{ guest_facts_0001['instance']['hw_product_uuid'] }}" @@ -154,3 +163,4 @@ - "guest_facts_0004['instance']['snapshots'][0]['name'] == 'snap1'" - "guest_facts_0004['instance']['snapshots'][1]['name'] == 'snap2'" - "guest_facts_0004['instance']['current_snapshot']['name'] == 'snap2'" + - "guest_facts_0002['instance']['hw_folder'] == vm1 | dirname"