From 3c389aee7369c254595eeb8fee06d023e21d51f3 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 9 May 2018 10:23:01 +0530 Subject: [PATCH] VMware: Add more robust logic to deal with VM moref (#39462) This fix adds more robust logic to deal with virtual machine managed object and its comparsion. Signed-off-by: Abhijeet Kasurde --- .../modules/cloud/vmware/vmware_guest_find.py | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_find.py b/lib/ansible/modules/cloud/vmware/vmware_guest_find.py index 6e870e0143..85ebabaacc 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest_find.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest_find.py @@ -7,9 +7,11 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -ANSIBLE_METADATA = {'metadata_version': '1.1', - 'status': ['preview'], - 'supported_by': 'community'} +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} DOCUMENTATION = ''' --- @@ -74,7 +76,7 @@ folders: from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native -from ansible.module_utils.vmware import PyVmomi, get_all_objs, vmware_argument_spec +from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, find_vm_by_id try: from pyVmomi import vim @@ -90,17 +92,24 @@ class PyVmomiHelper(PyVmomi): def getvm_folder_paths(self): results = [] + vms = [] + + if self.uuid: + vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid") + if vm_obj is None: + self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid) + vms = [vm_obj] + + elif self.name: + objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name']) + for temp_vm_object in objects: + if temp_vm_object.obj.name == self.name: + vms.append(temp_vm_object.obj) + + for vm in vms: + folder_path = self.get_vm_path(self.content, vm) + results.append(folder_path) - # compare the folder path of each VM against the search path - vmList = get_all_objs(self.content, [vim.VirtualMachine]) - for item in vmList.items(): - vobj = item[0] - if not isinstance(vobj.parent, vim.Folder): - continue - # Match by name or uuid - if vobj.config.name == self.name or vobj.config.uuid == self.uuid: - folderpath = self.get_vm_path(self.content, vobj) - results.append(folderpath) return results