diff --git a/changelogs/fragments/47563-vmware_host-refactor_SSL_thumbprint.yaml b/changelogs/fragments/47563-vmware_host-refactor_SSL_thumbprint.yaml new file mode 100644 index 0000000000..f97a5dacef --- /dev/null +++ b/changelogs/fragments/47563-vmware_host-refactor_SSL_thumbprint.yaml @@ -0,0 +1,2 @@ +bugfixes: +- vmware_host - fixes the retry mechanism of AddHost task. diff --git a/lib/ansible/module_utils/vmware.py b/lib/ansible/module_utils/vmware.py index 093d44ca14..213e7e9d58 100644 --- a/lib/ansible/module_utils/vmware.py +++ b/lib/ansible/module_utils/vmware.py @@ -32,7 +32,8 @@ from ansible.module_utils.basic import env_fallback class TaskError(Exception): - pass + def __init__(self, *args, **kwargs): + super(TaskError, self).__init__(*args, **kwargs) def wait_for_task(task, max_backoff=64, timeout=3600): @@ -56,12 +57,15 @@ def wait_for_task(task, max_backoff=64, timeout=3600): return True, task.info.result if task.info.state == vim.TaskInfo.State.error: error_msg = task.info.error + host_thumbprint = None try: error_msg = error_msg.msg + if hasattr(task.info.error, 'thumbprint'): + host_thumbprint = task.info.error.thumbprint except AttributeError: pass finally: - raise_from(TaskError(error_msg), task.info.error) + raise_from(TaskError(error_msg, host_thumbprint), task.info.error) if task.info.state in [vim.TaskInfo.State.running, vim.TaskInfo.State.queued]: sleep_time = min(2 ** failure_counter + randint(1, 1000) / 1000, max_backoff) time.sleep(sleep_time) diff --git a/lib/ansible/modules/cloud/vmware/vmware_host.py b/lib/ansible/modules/cloud/vmware/vmware_host.py index 4dd7e29cf5..c8a6b4cc6a 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_host.py +++ b/lib/ansible/modules/cloud/vmware/vmware_host.py @@ -259,10 +259,14 @@ class VMwareHost(PyVmomi): return success, result except TaskError as task_error_exception: task_error = task_error_exception.args[0] - if self.esxi_ssl_thumbprint == '' and isinstance(task_error, vim.fault.SSLVerifyFault): + if len(task_error_exception.args) == 2: + host_thumbprint = task_error_exception.args[1] + else: + host_thumbprint = None + if self.esxi_ssl_thumbprint == '' and host_thumbprint: # User has not specified SSL Thumbprint for ESXi host, # try to grab it using SSLVerifyFault exception - host_connect_spec.sslThumbprint = task_error.thumbprint + host_connect_spec.sslThumbprint = host_thumbprint else: self.module.fail_json(msg="Failed to add host %s to vCenter: %s" % (self.esxi_hostname, to_native(task_error)))