# Buggy code
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
    just hang forever waiting for more commands.)
    """

    display.debug("Sending initial data")

    try:
        fh.write(to_bytes(in_data))
        fh.close()
    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(
                '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))


# Traceback (UnicodeDecodeError)
TASK [stat] *************************************************************************************************************************************************************
task path: /home/jenkins/playbook.yml:3
Friday 14 June 2019  10:36:17 +0200 (0:00:00.170)       0:00:00.170 ***********
Using module file /jenkins/test/ansible/lib/python2.6/site-packages/ansible/modules/files/stat.py
<server> ESTABLISH SSH CONNECTION FOR USER: myuser
<server> SSH: EXEC ssh -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=myuser -o ConnectTimeout=20 server '/bin/sh -c '"'"'/usr/bin/python &amp;&amp; sleep 0'"'"''
<server> (1, '\n{"msg": "Permission denied", "failed": true, "exception": "  File \\"/tmp/ansible_Vvkdbf/ansible_module_stat.py\\", line 476, in main\\n    st = os.lstat(b_path)\\n", "invocation": {"module_args": {"checksum_algorithm": "sha1", "get_checksum": true, "follow": false, "path": "/home/anotheruser/lvi", "get_md5": null, "get_mime": true, "get_attributes": true}}}\n', "ATTENTION:\nQuiconque acc\xc3\xa9derait ou tenterait d'acc\xc3\xa9der \xc3\xa0 ce syst\xc3\xa8me.\n\n")
The full traceback is:
Traceback (most recent call last):
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/executor/task_executor.py", line 139, in run
res = self._execute()
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/executor/task_executor.py", line 584, in _execute
result = self._handler.run(task_vars=variables)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/plugins/action/normal.py", line 46, in run
result = merge_hash(result, self._execute_module(task_vars=task_vars, wrap_async=wrap_async))
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/plugins/action/__init__.py", line 853, in _execute_module
res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/plugins/action/__init__.py", line 960, in _low_level_execute_command
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/plugins/connection/ssh.py", line 1080, in exec_command
(returncode, stdout, stderr) = self._run(cmd, in_data, sudoable=sudoable)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/plugins/connection/ssh.py", line 354, in wrapped
_handle_error(remaining_retries, cmd[0], return_tuple, self._play_context.no_log, self.host)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/plugins/connection/ssh.py", line 307, in _handle_error
display.vvv(msg, host=host)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/utils/display.py", line 183, in vvv
return self.verbose(msg, host=host, caplevel=2)
File "/jenkins/test/ansible/lib/python2.6/site-packages/ansible/utils/display.py", line 206, in verbose
self.display("<%s> %s" % (host, msg), color=C.COLOR_VERBOSE, screen_only=True)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 63: ordinal not in range(128)

fatal: [server]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}

# Fixed code
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
    just hang forever waiting for more commands.)
    """

    display.debug("Sending initial data")

    try:
        fh.write(to_bytes(in_data))
        fh.close()
    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(
                '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))