diff --git a/changelogs/fragments/27-guest-info-vm-name-bug.yml b/changelogs/fragments/27-guest-info-vm-name-bug.yml new file mode 100644 index 00000000..f3e03af0 --- /dev/null +++ b/changelogs/fragments/27-guest-info-vm-name-bug.yml @@ -0,0 +1,2 @@ +bugfixes: + - guest_info - Fixed bugs that caused module failure when specifying the guest_name attribute diff --git a/plugins/module_utils/vmware_rest_client.py b/plugins/module_utils/vmware_rest_client.py index 4295d5d7..4646cecf 100644 --- a/plugins/module_utils/vmware_rest_client.py +++ b/plugins/module_utils/vmware_rest_client.py @@ -41,7 +41,8 @@ ResourcePool, Datastore, Cluster, - Host) + Host, + VM) HAS_VSPHERE = True except ImportError: VSPHERE_IMP_ERR = traceback.format_exc() @@ -340,6 +341,26 @@ def get_vm_tags(self, tag_service=None, tag_association_svc=None, vm_mid=None): return tags + def get_vm_by_name(self, name): + """ + Returns a VM object that matches the given name. + + Args: + name (str): The name of VM to look for + + Returns: + list(str): VM object matching the name provided. Returns None if no + matches are found + """ + vms = self.api_client.vcenter.VM.list( + VM.FilterSpec(names=set([name])) + ) + + if len(vms) == 0: + return None + + return vms[0] + def get_library_item_by_name(self, name): """ Returns the identifier of the library item with the given name. diff --git a/plugins/modules/guest_info.py b/plugins/modules/guest_info.py index d335098b..c937520e 100644 --- a/plugins/modules/guest_info.py +++ b/plugins/modules/guest_info.py @@ -8,7 +8,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type - DOCUMENTATION = r''' --- module: guest_info @@ -114,7 +113,14 @@ def get_guest_info(self): guests = [] if self.params.get('guest_name'): - vms = self._get_vm(self.params.get('guest_name')) + matching_vms = self.vmware_client.get_vm_by_name( + name=self.params.get('guest_name') + ) + try: + _ = iter(matching_vms) # pylint: disable=disallowed-name + vms = matching_vms + except TypeError: + vms = [] if not matching_vms else [matching_vms] else: vms = self.vmware_client.api_client.vcenter.VM.list() @@ -137,17 +143,6 @@ def _vvars(self, vmware_obj, r): else: r[k] = str(v) - def _get_vm(self, vm_name): - names = set([vm_name]) - vms = self.vmware_client.api_client.vcenter.VM.list( - self.vmware_client.api_client.VM.FilterSpec(names=names) - ) - - if len(vms) == 0: - return None - - return vms[0] - def main(): argument_spec = VmwareRestClient.vmware_client_argument_spec()