From 6d13acf1fff4d2a8acf1da64270bdf44f746c77d Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Wed, 30 Jan 2019 09:40:21 +1000 Subject: [PATCH] Ignore AttributeError when trying to import p paramiko (#51243) * Ignore AttributeError when trying to import p paramiko * preserve import error --- lib/ansible/plugins/connection/netconf.py | 8 +++++--- lib/ansible/plugins/connection/paramiko_ssh.py | 7 ++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/ansible/plugins/connection/netconf.py b/lib/ansible/plugins/connection/netconf.py index 95987fbff1..e71303a535 100644 --- a/lib/ansible/plugins/connection/netconf.py +++ b/lib/ansible/plugins/connection/netconf.py @@ -206,8 +206,10 @@ try: from ncclient.transport.errors import SSHUnknownHostError from ncclient.xml_ import to_ele, to_xml HAS_NCCLIENT = True -except ImportError: + NCCLIENT_IMP_ERR = None +except (ImportError, AttributeError) as err: # paramiko and gssapi are incompatible and raise AttributeError not ImportError HAS_NCCLIENT = False + NCCLIENT_IMP_ERR = err logging.getLogger('ncclient').setLevel(logging.INFO) @@ -270,8 +272,8 @@ class Connection(NetworkConnectionBase): def _connect(self): if not HAS_NCCLIENT: raise AnsibleError( - 'ncclient is required to use the netconf connection type.\n' - 'Please run pip install ncclient' + 'ncclient is required to use the netconf connection type: %s.\n' + 'Please run pip install ncclient' % to_native(NCCLIENT_IMP_ERR) ) self.queue_message('log', 'ssh connection done, starting ncclient') diff --git a/lib/ansible/plugins/connection/paramiko_ssh.py b/lib/ansible/plugins/connection/paramiko_ssh.py index dbcb734b32..4829a23849 100644 --- a/lib/ansible/plugins/connection/paramiko_ssh.py +++ b/lib/ansible/plugins/connection/paramiko_ssh.py @@ -168,13 +168,14 @@ SETTINGS_REGEX = re.compile(r'(\w+)(?:\s*=\s*|\s+)(.+)') # prevent paramiko warning noise -- see http://stackoverflow.com/questions/3920502/ HAVE_PARAMIKO = False +PARAMIKO_IMP_ERR = None with warnings.catch_warnings(): warnings.simplefilter("ignore") try: import paramiko HAVE_PARAMIKO = True - except ImportError: - pass + except (ImportError, AttributeError) as err: # paramiko and gssapi are incompatible and raise AttributeError not ImportError + PARAMIKO_IMP_ERR = err class MyAddPolicy(object): @@ -305,7 +306,7 @@ class Connection(ConnectionBase): ''' activates the connection object ''' if not HAVE_PARAMIKO: - raise AnsibleError("paramiko is not installed") + raise AnsibleError("paramiko is not installed: %s" % to_native(PARAMIKO_IMP_ERR)) port = self._play_context.port or 22 display.vvv("ESTABLISH PARAMIKO SSH CONNECTION FOR USER: %s on PORT %s TO %s" % (self._play_context.remote_user, port, self._play_context.remote_addr),