Skip to content

Commit

Permalink
Preserve ssh error (ansible#56460)
Browse files Browse the repository at this point in the history
* Preserve ssh error

* more details on fail

* removed redundant caption to errors

(cherry picked from commit 22b9525)
  • Loading branch information
bcoca committed May 23, 2019
1 parent ecef18d commit d52dc42
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/ssh_better_errors.yml
@@ -0,0 +1,2 @@
bugfixes:
- preserve actual ssh error when we cannot connect.
20 changes: 12 additions & 8 deletions lib/ansible/plugins/connection/ssh.py
Expand Up @@ -413,7 +413,7 @@ def wrapped(self, *args, **kwargs):
break

# 5 = Invalid/incorrect password from sshpass
except AnsibleAuthenticationFailure as e:
except AnsibleAuthenticationFailure:
# Raising this exception, which is subclassed from AnsibleConnectionFailure, prevents further retries
raise

Expand Down Expand Up @@ -686,15 +686,15 @@ def _send_initial_data(self, fh, in_data, ssh_process):
try:
fh.write(to_bytes(in_data))
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
# Only raise AnsibleConnectionFailure if the ssh process is still alive
time.sleep(0.001)
ssh_process.poll()
if getattr(ssh_process, 'returncode', None) is None:
raise AnsibleConnectionFailure(
'SSH Error: data could not be sent to remote host "%s". Make sure this host can be reached '
'over ssh' % self.host
'Data could not be sent to remote host "%s". Make sure this host can be reached '
'over ssh: %s' % (self.host, to_native(e)), orig_exc=e
)

display.debug('Sent initial data (%d bytes)' % len(in_data))
Expand Down Expand Up @@ -1030,11 +1030,15 @@ def _bare_run(self, cmd, in_data, sudoable=True, checkrc=True):
# 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.
controlpersist_broken_pipe = b'mux_client_hello_exchange: write packet: Broken pipe' in b_stderr
if p.returncode == 255 and controlpersist_broken_pipe:
raise AnsibleControlPersistBrokenPipeError('SSH Error: data could not be sent because of ControlPersist broken pipe.')
if p.returncode == 255:

additional = to_native(b_stderr)
if controlpersist_broken_pipe:
raise AnsibleControlPersistBrokenPipeError('Data could not be sent because of ControlPersist broken pipe: %s' % additional)

if p.returncode == 255 and in_data and checkrc:
raise AnsibleConnectionFailure('SSH Error: data could not be sent to remote host "%s". Make sure this host can be reached over ssh' % self.host)
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)

Expand Down

0 comments on commit d52dc42

Please sign in to comment.