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


# Traceback (ValueError)
2016-01-26 16:41:22,990 p=8627 u=root |  ^[[0;31mAn exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ansible/executor/process/worker.py", line 114, in run
self._shared_loader_obj,
File "/usr/local/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 91, in run
item_results = self._run_loop(items)
File "/usr/local/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 241, in _run_loop
res = self._execute(variables=task_vars)
File "/usr/local/lib/python2.7/dist-packages/ansible/executor/task_executor.py", line 401, in _execute
result = self._handler.run(task_vars=variables)
File "/usr/local/lib/python2.7/dist-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/local/lib/python2.7/dist-packages/ansible/plugins/action/__init__.py", line 395, in _execute_module
tmp = self._make_tmp_path()
File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/action/__init__.py", line 210, in _make_tmp_path
result = self._low_level_execute_command(cmd, sudoable=False)
File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/action/__init__.py", line 510, in _low_level_execute_command
rc, stdout, stderr = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/connection/ssh.py", line 600, in exec_command
return_tuple = self._exec_command(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/connection/ssh.py", line 577, in _exec_command
(returncode, stdout, stderr) = self._run(cmd, in_data, sudoable=sudoable)
File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/connection/ssh.py", line 417, in _run
rfd, wfd, efd = select.select(rpipes, [], [], timeout)
ValueError: filedescriptor out of range in select()

# 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,
        )
        selector = selectors.DefaultSelector()
        selector.register(p.stdout, selectors.EVENT_READ)
        selector.register(p.stderr, selectors.EVENT_READ)

        become_output = b""
        try:
            while not self.check_become_success(
                become_output
            ) and not self.check_password_prompt(become_output):
                events = selector.select(self._play_context.timeout)
                if not events:
                    stdout, stderr = p.communicate()
                    raise AnsibleError(
                        "timeout waiting for privilege escalation password prompt:\n"
                        + to_native(become_output)
                    )

                for key, event in events:
                    if key.fileobj == p.stdout:
                        chunk = p.stdout.read()
                    elif key.fileobj == p.stderr:
                        chunk = p.stderr.read()

                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
        finally:
            selector.close()

        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)