# 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(
                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)


# Traceback (UnicodeEncodeError)
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/executor/task_executor.py", line 96, in run
item_results = self._run_loop(items)
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/executor/task_executor.py", line 252, in _run_loop
res = self._execute(variables=task_vars)
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/executor/task_executor.py", line 447, in _execute
result = self._handler.run(task_vars=variables)
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/action/normal.py", line 33, in run
results = merge_hash(results, self._execute_module(tmp=tmp, task_vars=task_vars))
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/action/__init__.py", line 647, in _execute_module
res = self._low_level_execute_command(cmd, sudoable=sudoable, in_data=in_data)
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/action/__init__.py", line 721, in _low_level_execute_command
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
File "/home/jon/Development/Personal/server-build/ansible/lib/ansible/plugins/connection/paramiko_ssh.py", line 311, in exec_command
chan.sendall(self._play_context.become_pass + '\n')
File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 797, in sendall
sent = self.send(s)
File "/usr/lib/python2.7/dist-packages/paramiko/channel.py", line 729, in send
m.add_string(s[:size])
File "/usr/lib/python2.7/dist-packages/paramiko/message.py", line 259, in add_string
self.packet.write(s)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 1: ordinal not in range(128)

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

# 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 = b""
        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"
                    + to_native(become_output)
                )
            if not chunk:
                stdout, stderr = p.communicate()
                raise AnsibleError(
                    "privilege output closed while waiting for password prompt:\n"
                    + to_native(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)