Preserve ssh error (#56460)

* Preserve ssh error

* more details on fail

* removed redundant caption to errors
This commit is contained in:
Brian Coca 2019-05-23 10:03:35 -04:00 committed by GitHub
parent 7f0bf0a98a
commit 22b9525aa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- preserve actual ssh error when we cannot connect.

View file

@ -413,7 +413,7 @@ def _ssh_retry(func):
break break
# 5 = Invalid/incorrect password from sshpass # 5 = Invalid/incorrect password from sshpass
except AnsibleAuthenticationFailure as e: except AnsibleAuthenticationFailure:
# Raising this exception, which is subclassed from AnsibleConnectionFailure, prevents further retries # Raising this exception, which is subclassed from AnsibleConnectionFailure, prevents further retries
raise raise
@ -686,15 +686,15 @@ class Connection(ConnectionBase):
try: try:
fh.write(to_bytes(in_data)) fh.write(to_bytes(in_data))
fh.close() fh.close()
except (OSError, IOError): except (OSError, IOError) as e:
# The ssh connection may have already terminated at this point, with a more useful error # The ssh connection may have already terminated at this point, with a more useful error
# Only raise AnsibleConnectionFailure if the ssh process is still alive # Only raise AnsibleConnectionFailure if the ssh process is still alive
time.sleep(0.001) time.sleep(0.001)
ssh_process.poll() ssh_process.poll()
if getattr(ssh_process, 'returncode', None) is None: if getattr(ssh_process, 'returncode', None) is None:
raise AnsibleConnectionFailure( raise AnsibleConnectionFailure(
'SSH Error: data could not be sent to remote host "%s". Make sure this host can be reached ' 'Data could not be sent to remote host "%s". Make sure this host can be reached '
'over ssh' % self.host 'over ssh: %s' % (self.host, to_native(e)), orig_exc=e
) )
display.debug('Sent initial data (%d bytes)' % len(in_data)) display.debug('Sent initial data (%d bytes)' % len(in_data))
@ -1030,11 +1030,15 @@ class Connection(ConnectionBase):
# If we find a broken pipe because of ControlPersist timeout expiring (see #16731), # If we find a broken pipe because of ControlPersist timeout expiring (see #16731),
# we raise a special exception so that we can retry a connection. # we raise a special exception so that we can retry a connection.
controlpersist_broken_pipe = b'mux_client_hello_exchange: write packet: Broken pipe' in b_stderr controlpersist_broken_pipe = b'mux_client_hello_exchange: write packet: Broken pipe' in b_stderr
if p.returncode == 255 and controlpersist_broken_pipe: if p.returncode == 255:
raise AnsibleControlPersistBrokenPipeError('SSH Error: data could not be sent because of ControlPersist broken pipe.')
if p.returncode == 255 and in_data and checkrc: additional = to_native(b_stderr)
raise AnsibleConnectionFailure('SSH Error: data could not be sent to remote host "%s". Make sure this host can be reached over ssh' % self.host) if controlpersist_broken_pipe:
raise AnsibleControlPersistBrokenPipeError('Data could not be sent because of ControlPersist broken pipe: %s' % additional)
elif in_data and checkrc:
raise AnsibleConnectionFailure('Data could not be sent to remote host "%s". Make sure this host can be reached over ssh: %s'
% (self.host, additional))
return (p.returncode, b_stdout, b_stderr) return (p.returncode, b_stdout, b_stderr)