# Buggy code
def exec_command(self, cmd, in_data=None, sudoable=True):
    """run a command on the local host"""

    super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)

    display.debug("in local.exec_command()")

    executable = C.DEFAULT_EXECUTABLE.split()[0] if C.DEFAULT_EXECUTABLE else None

    display.vvv("EXEC {0}".format(cmd), host=self._play_context.remote_addr)
    display.debug("opening command with Popen()")

    if isinstance(cmd, (text_type, binary_type)):
        cmd = to_bytes(cmd)
    else:
        cmd = map(to_bytes, cmd)

    p = subprocess.Popen(
        cmd,
        shell=isinstance(cmd, (text_type, binary_type)),
        executable=executable,  # cwd=...
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    display.debug("done running command with Popen()")

    if self._play_context.prompt and sudoable:
        fcntl.fcntl(
            p.stdout,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stdout, fcntl.F_GETFL) | os.O_NONBLOCK,
        )
        fcntl.fcntl(
            p.stderr,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stderr, fcntl.F_GETFL) | os.O_NONBLOCK,
        )
        become_output = ""
        while not self.check_become_success(
            become_output
        ) and not self.check_password_prompt(become_output):
            rfd, wfd, efd = select.select(
                [p.stdout, p.stderr],
                [],
                [p.stdout, p.stderr],
                self._play_context.timeout,
            )
            if p.stdout in rfd:
                chunk = p.stdout.read()
            elif p.stderr in rfd:
                chunk = p.stderr.read()
            else:
                stdout, stderr = p.communicate()
                raise AnsibleError(
                    "timeout waiting for privilege escalation password prompt:\n"
                    + become_output
                )
            if not chunk:
                stdout, stderr = p.communicate()
                raise AnsibleError(
                    "privilege output closed while waiting for password prompt:\n"
                    + become_output
                )
            become_output += chunk
        if not self.check_become_success(become_output):
            p.stdin.write(self._play_context.become_pass + "\n")
        fcntl.fcntl(
            p.stdout,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stdout, fcntl.F_GETFL) & ~os.O_NONBLOCK,
        )
        fcntl.fcntl(
            p.stderr,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stderr, fcntl.F_GETFL) & ~os.O_NONBLOCK,
        )

    display.debug("getting output with communicate()")
    stdout, stderr = p.communicate(in_data)
    display.debug("done communicating")

    display.debug("done with local.exec_command()")
    return (p.returncode, stdout, stderr)


# Traceback (UnicodeEncodeError)
[USER@SYSTEM ansible]$ ansible-playbook some_playbook.yml -K -vvv
Using /home/USER/git/ansible/ansible.cfg as config file
SUDO password:

PLAY [system01] ****************************************************************

TASK [fetch from system] *******************************************************
task path: /home/USER/git/ansible/some_playbook.yml:32
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: USER
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /tmp/ansible-tmp-1476453804.28-165187216012413 `" &amp;&amp; echo ansible-tmp-1476453804.28-165187216012413="` echo /tmp/ansible-tmp-1476453804.28-165187216012413 `" ) &amp;&amp; sleep 0'
<localhost> PUT /tmp/tmpWVAqrj TO /tmp/ansible-tmp-1476453804.28-165187216012413/command
<localhost> EXEC /bin/sh -c 'chown -R sysauto /tmp/ansible-tmp-1476453804.28-165187216012413/ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c 'setfacl -R -m u:sysauto:rX /tmp/ansible-tmp-1476453804.28-165187216012413/ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c 'sudo -H -S  -p "[sudo via ansible, key=opbxnljylhudewlchnsbwfnibhzyaent] password: " -u sysauto /bin/sh -c '"'"'echo BECOME-SUCCESS-opbxnljylhudewlchnsbwfnibhzyaent; LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /tmp/ansible-tmp-1476453804.28-165187216012413/command'"'"' &amp;&amp; sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 124, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 446, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/normal.py", line 33, in run
results = merge_hash(results, self._execute_module(tmp=tmp, task_vars=task_vars))
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py", line 644, in _execute_module
res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)
File "/usr/lib/python2.7/site-packages/ansible/plugins/action/__init__.py", line 718, in _low_level_execute_command
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
File "/usr/lib/python2.7/site-packages/ansible/plugins/connection/local.py", line 109, in exec_command
p.stdin.write(self._play_context.become_pass + '\n')
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 0: ordinal not in range(128)

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

NO MORE HOSTS LEFT *************************************************************

PLAY RECAP *********************************************************************
system01                   : ok=0    changed=0    unreachable=0    failed=

# Fixed code
def exec_command(self, cmd, in_data=None, sudoable=True):
    """run a command on the local host"""

    super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable)

    display.debug("in local.exec_command()")

    executable = C.DEFAULT_EXECUTABLE.split()[0] if C.DEFAULT_EXECUTABLE else None

    display.vvv("EXEC {0}".format(cmd), host=self._play_context.remote_addr)
    display.debug("opening command with Popen()")

    if isinstance(cmd, (text_type, binary_type)):
        cmd = to_bytes(cmd)
    else:
        cmd = map(to_bytes, cmd)

    p = subprocess.Popen(
        cmd,
        shell=isinstance(cmd, (text_type, binary_type)),
        executable=executable,  # cwd=...
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    display.debug("done running command with Popen()")

    if self._play_context.prompt and sudoable:
        fcntl.fcntl(
            p.stdout,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stdout, fcntl.F_GETFL) | os.O_NONBLOCK,
        )
        fcntl.fcntl(
            p.stderr,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stderr, fcntl.F_GETFL) | os.O_NONBLOCK,
        )
        become_output = ""
        while not self.check_become_success(
            become_output
        ) and not self.check_password_prompt(become_output):
            rfd, wfd, efd = select.select(
                [p.stdout, p.stderr],
                [],
                [p.stdout, p.stderr],
                self._play_context.timeout,
            )
            if p.stdout in rfd:
                chunk = p.stdout.read()
            elif p.stderr in rfd:
                chunk = p.stderr.read()
            else:
                stdout, stderr = p.communicate()
                raise AnsibleError(
                    "timeout waiting for privilege escalation password prompt:\n"
                    + become_output
                )
            if not chunk:
                stdout, stderr = p.communicate()
                raise AnsibleError(
                    "privilege output closed while waiting for password prompt:\n"
                    + become_output
                )
            become_output += chunk
        if not self.check_become_success(become_output):
            p.stdin.write(
                to_bytes(self._play_context.become_pass, errors="surrogate_or_strict")
                + b"\n"
            )
        fcntl.fcntl(
            p.stdout,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stdout, fcntl.F_GETFL) & ~os.O_NONBLOCK,
        )
        fcntl.fcntl(
            p.stderr,
            fcntl.F_SETFL,
            fcntl.fcntl(p.stderr, fcntl.F_GETFL) & ~os.O_NONBLOCK,
        )

    display.debug("getting output with communicate()")
    stdout, stderr = p.communicate(in_data)
    display.debug("done communicating")

    display.debug("done with local.exec_command()")
    return (p.returncode, stdout, stderr)