diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index 58e2517075..7504a73d5e 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -1489,21 +1489,11 @@ class PyVmomiHelper(PyVmomi): self.change_detected = True def select_host(self): - # if the user wants a cluster, get the list of hosts for the cluster and use the first one - if self.params['cluster']: - cluster = self.cache.get_cluster(self.params['cluster']) - if not cluster: - self.module.fail_json(msg='Failed to find cluster "%(cluster)s"' % self.params) - hostsystems = [x for x in cluster.host] - if not hostsystems: - self.module.fail_json(msg='No hosts found in cluster "%(cluster)s. Maybe you lack the right privileges ?"' % self.params) - # TODO: add a policy to select host - hostsystem = hostsystems[0] - else: - hostsystem = self.cache.get_esx_host(self.params['esxi_hostname']) - if not hostsystem: - self.module.fail_json(msg='Failed to find ESX host "%(esxi_hostname)s"' % self.params) - + hostsystem = self.cache.get_esx_host(self.params['esxi_hostname']) + if not hostsystem: + self.module.fail_json(msg='Failed to find ESX host "%(esxi_hostname)s"' % self.params) + if hostsystem.runtime.connectionState != 'connected' or hostsystem.runtime.inMaintenanceMode: + self.module.fail_json(msg='ESXi "%(esxi_hostname)s" is in invalid state or in maintenance mode.' % self.params) return hostsystem def autoselect_datastore(self):