# Buggy code
def _start_connection(self, variables):
    """
    Starts the persistent connection
    """
    master, slave = pty.openpty()

    python = sys.executable

    def find_file_in_path(filename):
        # Check $PATH first, followed by same directory as sys.argv[0]
        paths = os.environ["PATH"].split(os.pathsep) + [os.path.dirname(sys.argv[0])]
        for dirname in paths:
            fullpath = os.path.join(dirname, filename)
            if os.path.isfile(fullpath):
                return fullpath

        raise AnsibleError("Unable to find location of '%s'" % filename)

    p = subprocess.Popen(
        [python, find_file_in_path("ansible-connection"), to_text(os.getppid())],
        stdin=slave,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    stdin = os.fdopen(master, "wb", 0)
    os.close(slave)

    # Need to force a protocol that is compatible with both py2 and py3.
    # That would be protocol=2 or less.
    # Also need to force a protocol that excludes certain control chars as
    # stdin in this case is a pty and control chars will cause problems.
    # that means only protocol=0 will work.
    src = cPickle.dumps(self._play_context.serialize(), protocol=0)
    stdin.write(src)
    stdin.write(b"\n#END_INIT#\n")

    src = cPickle.dumps(variables, protocol=0)
    # remaining \r fail to round-trip the socket
    src = src.replace(b"\r", rb"\r")
    stdin.write(src)
    stdin.write(b"\n#END_VARS#\n")

    stdin.flush()

    (stdout, stderr) = p.communicate()
    stdin.close()

    if p.returncode == 0:
        result = json.loads(to_text(stdout, errors="surrogate_then_replace"))
    else:
        try:
            result = json.loads(to_text(stderr, errors="surrogate_then_replace"))
        except getattr(json.decoder, "JSONDecodeError", ValueError):
            # JSONDecodeError only available on Python 3.5+
            result = {"error": to_text(stderr, errors="surrogate_then_replace")}

    if "messages" in result:
        for msg in result.get("messages"):
            display.vvvv("%s" % msg, host=self._play_context.remote_addr)

    if "error" in result:
        if self._play_context.verbosity > 2:
            if result.get("exception"):
                msg = "The full traceback is:\n" + result["exception"]
                display.display(msg, color=C.COLOR_ERROR)
        raise AnsibleError(result["error"])

    return result["socket_path"]


# 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 _start_connection(self, variables):
    """
    Starts the persistent connection
    """
    master, slave = pty.openpty()

    python = sys.executable

    def find_file_in_path(filename):
        # Check $PATH first, followed by same directory as sys.argv[0]
        paths = os.environ["PATH"].split(os.pathsep) + [os.path.dirname(sys.argv[0])]
        for dirname in paths:
            fullpath = os.path.join(dirname, filename)
            if os.path.isfile(fullpath):
                return fullpath

        raise AnsibleError("Unable to find location of '%s'" % filename)

    p = subprocess.Popen(
        [python, find_file_in_path("ansible-connection"), to_text(os.getppid())],
        stdin=slave,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
    )
    os.close(slave)

    # We need to set the pty into noncanonical mode. This ensures that we
    # can receive lines longer than 4095 characters (plus newline) without
    # truncating.
    old = termios.tcgetattr(master)
    new = termios.tcgetattr(master)
    new[3] = new[3] & ~termios.ICANON

    try:
        termios.tcsetattr(master, termios.TCSANOW, new)
        write_to_file_descriptor(master, variables)
        write_to_file_descriptor(master, self._play_context.serialize())

        (stdout, stderr) = p.communicate()
    finally:
        termios.tcsetattr(master, termios.TCSANOW, old)
    os.close(master)

    if p.returncode == 0:
        result = json.loads(to_text(stdout, errors="surrogate_then_replace"))
    else:
        try:
            result = json.loads(to_text(stderr, errors="surrogate_then_replace"))
        except getattr(json.decoder, "JSONDecodeError", ValueError):
            # JSONDecodeError only available on Python 3.5+
            result = {"error": to_text(stderr, errors="surrogate_then_replace")}

    if "messages" in result:
        for msg in result.get("messages"):
            display.vvvv("%s" % msg, host=self._play_context.remote_addr)

    if "error" in result:
        if self._play_context.verbosity > 2:
            if result.get("exception"):
                msg = "The full traceback is:\n" + result["exception"]
                display.display(msg, color=C.COLOR_ERROR)
        raise AnsibleError(result["error"])

    return result["socket_path"]