Update connection logic in vmware module_utils (#26323)

Fix updates logic of connection in vmware module_utils

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/28326/head
Abhijeet Kasurde 7 years ago committed by Dag Wieers
parent e7a78f4328
commit 86f23dc620

@ -384,7 +384,6 @@ def vmware_argument_spec():
def connect_to_api(module, disconnect_atexit=True): def connect_to_api(module, disconnect_atexit=True):
hostname = module.params['hostname'] hostname = module.params['hostname']
username = module.params['username'] username = module.params['username']
password = module.params['password'] password = module.params['password']
@ -394,21 +393,23 @@ def connect_to_api(module, disconnect_atexit=True):
module.fail_json(msg='pyVim does not support changing verification mode with python < 2.7.9. Either update ' module.fail_json(msg='pyVim does not support changing verification mode with python < 2.7.9. Either update '
'python or or use validate_certs=false') 'python or or use validate_certs=false')
ssl_context = None
if not validate_certs:
ssl_context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ssl_context.verify_mode = ssl.CERT_NONE
service_instance = None
try: try:
service_instance = connect.SmartConnect(host=hostname, user=username, pwd=password) service_instance = connect.SmartConnect(host=hostname, user=username, pwd=password, sslContext=ssl_context)
except vim.fault.InvalidLogin as invalid_login: except vim.fault.InvalidLogin as e:
module.fail_json(msg=invalid_login.msg, apierror=str(invalid_login)) module.fail_json(msg="Unable to log on to vCenter or ESXi API at %s as %s: %s" % (hostname, username, e.msg))
except (requests.ConnectionError, ssl.SSLError) as connection_error: except (requests.ConnectionError, ssl.SSLError) as e:
if '[SSL: CERTIFICATE_VERIFY_FAILED]' in str(connection_error) and not validate_certs: module.fail_json(msg="Unable to connect to vCenter or ESXi API at %s on TCP/443: %s" % (hostname, e))
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) except Exception as e:
context.verify_mode = ssl.CERT_NONE module.fail_json(msg="Unknown error connecting to vCenter or ESXi API at %s: %s" % (hostname, e))
service_instance = connect.SmartConnect(host=hostname, user=username, pwd=password, sslContext=context)
else: if service_instance is None:
module.fail_json(msg="Unable to connect to vCenter or ESXi API on TCP/443.", apierror=str(connection_error)) module.fail_json(msg="Unknown error connecting to vCenter or ESXi API at %s" % hostname)
except:
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
service_instance = connect.SmartConnect(host=hostname, user=username, pwd=password, sslContext=context)
# Disabling atexit should be used in special cases only. # Disabling atexit should be used in special cases only.
# Such as IP change of the ESXi host which removes the connection anyway. # Such as IP change of the ESXi host which removes the connection anyway.

Loading…
Cancel
Save