From 2007a79952f845b54821979c2ed3ae1e5f71e612 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Tue, 7 May 2019 17:05:38 +0530 Subject: [PATCH] VMware: Add managed object id in VM facts (#53523) Fixes: #53372 Signed-off-by: Abhijeet Kasurde --- .../53372-vmware_guest_facts-add_moid.yml | 2 ++ lib/ansible/module_utils/vmware.py | 9 +++++++++ .../cloud/vmware/vmware_guest_facts.py | 19 +++++++++++++++++-- .../targets/vmware_guest_facts/tasks/main.yml | 6 ++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/53372-vmware_guest_facts-add_moid.yml diff --git a/changelogs/fragments/53372-vmware_guest_facts-add_moid.yml b/changelogs/fragments/53372-vmware_guest_facts-add_moid.yml new file mode 100644 index 00000000000..cbed9c4ef1d --- /dev/null +++ b/changelogs/fragments/53372-vmware_guest_facts-add_moid.yml @@ -0,0 +1,2 @@ +minor_changes: +- Add managed object identifier (moId) and vim reference (vimref) of virtual machine in guest facts (https://github.com/ansible/ansible/issues/53372). diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index 9d5b08239e9..6dabbbbd031 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -302,6 +302,8 @@ def gather_vm_facts(content, vm): 'snapshots': [], 'current_snapshot': None, 'vnc': {}, + 'moid': vm._moId, + 'vimref': "vim.VirtualMachine:%s" % vm._moId, } # facts that may or may not exist @@ -1378,6 +1380,13 @@ class PyVmomi(object): self._deepmerge(result, tmp) else: result[prop] = self._jsonify(getattr(obj, prop)) + # To match gather_vm_facts output + prop_name = prop + if prop.lower() == '_moid': + prop_name = 'moid' + elif prop.lower() == '_vimref': + prop_name = 'vimref' + result[prop_name] = result[prop] except (AttributeError, KeyError): self.module.fail_json(msg="Property '{0}' not found.".format(prop)) else: diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py b/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py index 922c51b7f31..f0ad0e7449f 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py @@ -133,6 +133,20 @@ EXAMPLES = ''' properties: ["config.hardware.memoryMB", "guest.disk", "overallStatus"] delegate_to: localhost register: facts + +- name: Gather Managed object ID (moid) from a guest using the vSphere API output schema for REST Calls + vmware_guest_facts: + hostname: "{{ vcenter_hostname }}" + username: "{{ vcenter_username }}" + password: "{{ vcenter_password }}" + validate_certs: no + datacenter: "{{ datacenter_name }}" + name: "{{ vm_name }}" + schema: "vsphere" + properties: + - _moId + delegate_to: localhost + register: moid_facts ''' RETURN = """ @@ -192,7 +206,9 @@ instance: "tags": [ "backup" ], - "vnc": {} + "vnc": {}, + "moid": "vm-42", + "vimref": "vim.VirtualMachine:vm-42" } """ @@ -251,7 +267,6 @@ def main(): instance = pyv.gather_facts(vm) else: instance = pyv.to_json(vm, module.params['properties']) - if module.params.get('tags'): if not HAS_VCLOUD: module.fail_json(msg="Unable to find 'vCloud Suite SDK' Python library which is required." diff --git a/test/integration/targets/vmware_guest_facts/tasks/main.yml b/test/integration/targets/vmware_guest_facts/tasks/main.yml index 576e1948d86..8ae1a9291a9 100644 --- a/test/integration/targets/vmware_guest_facts/tasks/main.yml +++ b/test/integration/targets/vmware_guest_facts/tasks/main.yml @@ -38,6 +38,8 @@ - "guest_facts_0001['instance']['hw_folder'] is defined" - "guest_facts_0001['instance']['guest_question'] is defined" - "guest_facts_0001['instance']['guest_consolidation_needed'] is defined" + - "guest_facts_0001['instance']['moid'] is defined" + - "guest_facts_0001['instance']['vimref'] is defined" - "'portgroup_portkey' in guest_facts_0001['instance']['hw_eth0']" - "'portgroup_key' in guest_facts_0001['instance']['hw_eth0']" - "guest_facts_0001['instance']['instance_uuid'] is defined" @@ -91,6 +93,8 @@ - "guest_facts_0002b['instance']['config']['hardware']['memoryMB'] is defined" - "guest_facts_0002b['instance']['config']['hardware']['numCoresPerSocket'] is not defined" - "guest_facts_0002b['instance']['guest']['toolsVersion'] is defined" + - "guest_facts_0001['instance']['moid'] is defined" + - "guest_facts_0001['instance']['vimref'] is defined" - "guest_facts_0002b['instance']['overallStatus'] is not defined" # Testcase 0003: Get details about virtual machines without snapshots using UUID @@ -182,3 +186,5 @@ - "guest_facts_0005['instance']['guest_consolidation_needed'] is defined" - "guest_facts_0005['instance']['instance_uuid'] is defined" - "guest_facts_0005['instance']['instance_uuid'] == vm1_instance_uuid" + - "guest_facts_0001['instance']['moid'] is defined" + - "guest_facts_0001['instance']['vimref'] is defined" \ No newline at end of file