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

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

    if in_data:
        raise AnsibleError(
            "Internal Error: this module does not support optimized module pipelining"
        )

    bufsize = 4096

    try:
        self.ssh.get_transport().set_keepalive(5)
        chan = self.ssh.get_transport().open_session()
    except Exception as e:
        msg = "Failed to open session"
        if len(str(e)) > 0:
            msg += ": %s" % str(e)
        raise AnsibleConnectionFailure(msg)

    # sudo usually requires a PTY (cf. requiretty option), therefore
    # we give it one by default (pty=True in ansble.cfg), and we try
    # to initialise from the calling environment when sudoable is enabled
    if self.get_option("pty") and sudoable:
        chan.get_pty(
            term=os.getenv("TERM", "vt100"),
            width=int(os.getenv("COLUMNS", 0)),
            height=int(os.getenv("LINES", 0)),
        )

    display.vvv("EXEC %s" % cmd, host=self._play_context.remote_addr)

    cmd = to_bytes(cmd, errors="surrogate_or_strict")

    no_prompt_out = b""
    no_prompt_err = b""
    become_output = b""

    try:
        chan.exec_command(cmd)
        if self._play_context.prompt:
            passprompt = False
            become_sucess = False
            while not (become_sucess or passprompt):
                display.debug("Waiting for Privilege Escalation input")

                chunk = chan.recv(bufsize)
                display.debug("chunk is: %s" % chunk)
                if not chunk:
                    if b"unknown user" in become_output:
                        raise AnsibleError(
                            "user %s does not exist" % self._play_context.become_user
                        )
                    else:
                        break
                        # raise AnsibleError('ssh connection closed waiting for password prompt')
                become_output += chunk

                # need to check every line because we might get lectured
                # and we might get the middle of a line in a chunk
                for l in become_output.splitlines(True):
                    if self.check_become_success(l):
                        become_sucess = True
                        break
                    elif self.check_password_prompt(l):
                        passprompt = True
                        break

            if passprompt:
                if self._play_context.become and self._play_context.become_pass:
                    chan.sendall(to_bytes(self._play_context.become_pass) + b"\n")
                else:
                    raise AnsibleError("A password is required but none was supplied")
            else:
                no_prompt_out += become_output
                no_prompt_err += become_output
    except socket.timeout:
        raise AnsibleError(
            "ssh timed out waiting for privilege escalation.\n" + become_output
        )

    stdout = b"".join(chan.makefile("rb", bufsize))
    stderr = b"".join(chan.makefile_stderr("rb", bufsize))

    return (chan.recv_exit_status(), no_prompt_out + stdout, no_prompt_out + stderr)


# Traceback (TypeError)
TASK [azure_load_balancer : Get lbs] *********************************************************************************************
task path: /home/vmadmin/cluster_build/roles/azure_load_balancer/tasks/main.yaml:15
Using module file /home/vmadmin/.local/lib/python2.7/site-packages/ansible/modules/cloud/azure/azure_rm_loadbalancer_facts.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: vmadmin
<localhost> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814 `" &amp;&amp; echo ansible-tmp-1504708358.3-41710485333814="` echo /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814 `" ) &amp;&amp; sleep 0'
<localhost> PUT /tmp/tmpy_CiqC TO /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/azure_rm_loadbalancer_facts.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/ /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/azure_rm_loadbalancer_facts.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/azure_rm_loadbalancer_facts.py; rm -rf "/home/vmadmin/.ansible/tmp/ansible-tmp-1504708358.3-41710485333814/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 180, in <module>
main()
File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 177, in main
AzureRMLoadBalancerFacts()
File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 123, in __init__
facts_module=True
File "/tmp/ansible_VTaHxN/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 255, in __init__
File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 133, in exec_module
else self.list_items()
File "/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py", line 162, in list_items
response = self.network_client.load_balancers.list()
TypeError: list() takes at least 2 arguments (1 given)

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 180, in <module>\n    main()\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 177, in main\n    AzureRMLoadBalancerFacts()\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 123, in __init__\n    facts_module=True\n  File \"/tmp/ansible_VTaHxN/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 255, in __init__\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 133, in exec_module\n    else self.list_items()\n  File \"/tmp/ansible_VTaHxN/ansible_module_azure_rm_loadbalancer_facts.py\", line 162, in list_items\n    response = self.network_client.load_balancers.list()\nTypeError: list() takes at least 2 arguments (1 given)\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

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

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

    if in_data:
        raise AnsibleError(
            "Internal Error: this module does not support optimized module pipelining"
        )

    bufsize = 4096

    try:
        self.ssh.get_transport().set_keepalive(5)
        chan = self.ssh.get_transport().open_session()
    except Exception as e:
        msg = "Failed to open session"
        if len(str(e)) > 0:
            msg += ": %s" % str(e)
        raise AnsibleConnectionFailure(msg)

    # sudo usually requires a PTY (cf. requiretty option), therefore
    # we give it one by default (pty=True in ansible.cfg), and we try
    # to initialise from the calling environment when sudoable is enabled
    if self.get_option("pty") and sudoable:
        chan.get_pty(
            term=os.getenv("TERM", "vt100"),
            width=int(os.getenv("COLUMNS", 0)),
            height=int(os.getenv("LINES", 0)),
        )

    display.vvv("EXEC %s" % cmd, host=self._play_context.remote_addr)

    cmd = to_bytes(cmd, errors="surrogate_or_strict")

    no_prompt_out = b""
    no_prompt_err = b""
    become_output = b""

    try:
        chan.exec_command(cmd)
        if self._play_context.prompt:
            passprompt = False
            become_sucess = False
            while not (become_sucess or passprompt):
                display.debug("Waiting for Privilege Escalation input")

                chunk = chan.recv(bufsize)
                display.debug("chunk is: %s" % chunk)
                if not chunk:
                    if b"unknown user" in become_output:
                        raise AnsibleError(
                            "user %s does not exist" % self._play_context.become_user
                        )
                    else:
                        break
                        # raise AnsibleError('ssh connection closed waiting for password prompt')
                become_output += chunk

                # need to check every line because we might get lectured
                # and we might get the middle of a line in a chunk
                for l in become_output.splitlines(True):
                    if self.check_become_success(l):
                        become_sucess = True
                        break
                    elif self.check_password_prompt(l):
                        passprompt = True
                        break

            if passprompt:
                if self._play_context.become and self._play_context.become_pass:
                    chan.sendall(to_bytes(self._play_context.become_pass) + b"\n")
                else:
                    raise AnsibleError("A password is required but none was supplied")
            else:
                no_prompt_out += become_output
                no_prompt_err += become_output
    except socket.timeout:
        raise AnsibleError(
            "ssh timed out waiting for privilege escalation.\n" + become_output
        )

    stdout = b"".join(chan.makefile("rb", bufsize))
    stderr = b"".join(chan.makefile_stderr("rb", bufsize))

    return (chan.recv_exit_status(), no_prompt_out + stdout, no_prompt_out + stderr)