Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't raise AnsibleConnectionFailure if the ssh process has already died. #53534

Merged
merged 3 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bugfixes:
- ssh - Check the return code of the ssh process before raising AnsibleConnectionFailure, as the error message
for the ssh process will likely contain more useful information. This will improve the missing interpreter messaging
when using modules such as setup which have a larger payload to transfer when combined with pipelining.
(https://github.com/ansible/ansible/issues/53487)
4 changes: 2 additions & 2 deletions lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,8 @@ def _parse_returned_data(self, res):

# try to figure out if we are missing interpreter
if self._used_interpreter is not None:
match = '%s: No such file or directory' % self._used_interpreter.lstrip('!#')
if match in data['module_stderr'] or match in data['module_stdout']:
match = re.compile('%s: (?:No such file or directory|not found)' % self._used_interpreter.lstrip('!#'))
if match.search(data['module_stderr']) or match.search(data['module_stdout']):
data['msg'] = "The module failed to execute correctly, you probably need to set the interpreter."

# always append hint
Expand Down
16 changes: 12 additions & 4 deletions lib/ansible/plugins/connection/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def _build_command(self, binary, *other_args):

return b_command

def _send_initial_data(self, fh, in_data):
def _send_initial_data(self, fh, in_data, ssh_process):
'''
Writes initial data to the stdin filehandle of the subprocess and closes
it. (The handle must be closed; otherwise, for example, "sftp -b -" will
Expand All @@ -676,7 +676,15 @@ def _send_initial_data(self, fh, in_data):
fh.write(to_bytes(in_data))
fh.close()
except (OSError, IOError):
raise AnsibleConnectionFailure('SSH Error: data could not be sent to remote host "%s". Make sure this host can be reached over ssh' % self.host)
# 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
)
sivel marked this conversation as resolved.
Show resolved Hide resolved

display.debug('Sent initial data (%d bytes)' % len(in_data))

Expand Down Expand Up @@ -853,7 +861,7 @@ def _bare_run(self, cmd, in_data, sudoable=True, checkrc=True):
# If we can send initial data without waiting for anything, we do so
# before we start polling
if states[state] == 'ready_to_send' and in_data:
self._send_initial_data(stdin, in_data)
self._send_initial_data(stdin, in_data, p)
state += 1

try:
Expand Down Expand Up @@ -967,7 +975,7 @@ def _bare_run(self, cmd, in_data, sudoable=True, checkrc=True):

if states[state] == 'ready_to_send':
if in_data:
self._send_initial_data(stdin, in_data)
self._send_initial_data(stdin, in_data, p)
state += 1

# Now we're awaiting_exit: has the child process exited? If it has,
Expand Down