# Buggy code
def main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        tenant=dict(
            type="str", aliases=["tenant_name"]
        ),  # Not required for querying all objects
        ap=dict(
            type="str", aliases=["app_profile", "app_profile_name"]
        ),  # Not required for querying all objects
        epg=dict(
            type="str", aliases=["epg_name"]
        ),  # Not required for querying all objects
        description=dict(type="str", aliases=["descr"]),
        encap_id=dict(type="int", aliases=["vlan", "vlan_id"]),
        primary_encap_id=dict(type="int", aliases=["primary_vlan", "primary_vlan_id"]),
        deploy_immediacy=dict(type="str", choices=["immediate", "lazy"]),
        interface_mode=dict(
            type="str",
            choices=[
                "802.1p",
                "access",
                "native",
                "regular",
                "tagged",
                "trunk",
                "untagged",
            ],
            aliases=["interface_mode_name", "mode"],
        ),
        interface_type=dict(
            type="str",
            default="switch_port",
            choices=["fex", "port_channel", "switch_port", "vpc"],
        ),
        pod_id=dict(
            type="int", aliases=["pod", "pod_number"]
        ),  # Not required for querying all objects
        leafs=dict(type="list", aliases=["leaves", "nodes", "paths", "switches"]),
        interface=dict(type="str"),
        extpaths=dict(type="int"),
        state=dict(
            type="str", default="present", choices=["absent", "present", "query"]
        ),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ["interface_type", "fex", ["extpaths"]],
            [
                "state",
                "absent",
                ["ap", "epg", "interface", "leafs", "pod_id", "tenant"],
            ],
            [
                "state",
                "present",
                ["ap", "encap_id", "epg", "interface", "leafs", "pod_id", "tenant"],
            ],
        ],
    )

    tenant = module.params["tenant"]
    ap = module.params["ap"]
    epg = module.params["epg"]
    description = module.params["description"]
    encap_id = module.params["encap_id"]
    primary_encap_id = module.params["primary_encap_id"]
    deploy_immediacy = module.params["deploy_immediacy"]
    interface_mode = module.params["interface_mode"]
    interface_type = module.params["interface_type"]
    pod_id = module.params["pod_id"]
    leafs = module.params["leafs"]
    if leafs is not None:
        # Users are likely to use integers for leaf IDs, which would raise an exception when using the join method
        leafs = [str(leaf) for leaf in module.params["leafs"]]
        if len(leafs) == 1:
            if interface_type != "vpc":
                leafs = leafs[0]
            else:
                module.fail_json(msg='A interface_type of "vpc" requires 2 leafs')
        elif len(leafs) == 2:
            if interface_type == "vpc":
                leafs = "-".join(leafs)
            else:
                module.fail_json(
                    msg='The interface_types "switch_port", "port_channel", and "fex" \
                    do not support using multiple leafs for a single binding'
                )
        else:
            module.fail_json(
                msg='The "leafs" parameter must not have more than 2 entries'
            )
    interface = module.params["interface"]
    extpaths = module.params["extpaths"]
    state = module.params["state"]
    static_path = ""

    if encap_id is not None:
        if encap_id in range(1, 4097):
            encap_id = "vlan-{0}".format(encap_id)
        else:
            module.fail_json(msg="Valid VLAN assigments are from 1 to 4096")

    if primary_encap_id is not None:
        if primary_encap_id in range(1, 4097):
            primary_encap_id = "vlan-{0}".format(primary_encap_id)
        else:
            module.fail_json(msg="Valid VLAN assigments are from 1 to 4096")

    INTERFACE_MODE_MAPPING = {
        "802.1p": "native",
        "access": "untagged",
        "native": "native",
        "regular": "regular",
        "tagged": "regular",
        "trunk": "regular",
        "untagged": "untagged",
    }

    INTERFACE_TYPE_MAPPING = dict(
        fex="topology/pod-{0}/paths-{1}/extpaths-{2}/pathep-[eth{3}]".format(
            pod_id, leafs, extpaths, interface
        ),
        port_channel="topology/pod-{0}/paths-{1}/pathep-[{2}]".format(
            pod_id, leafs, interface
        ),
        switch_port="topology/pod-{0}/paths-{1}/pathep-[eth{2}]".format(
            pod_id, leafs, interface
        ),
        vpc="topology/pod-{0}/protpaths-{1}/pathep-[{2}]".format(
            pod_id, leafs, interface
        ),
    )

    static_path = INTERFACE_TYPE_MAPPING[interface_type]

    path_target_filter = {}
    if (
        pod_id is not None
        and leafs is not None
        and interface is not None
        and (interface_type != "fex" or extpaths is not None)
    ):
        path_target_filter = {"tDn": static_path}

    if interface_mode is not None:
        interface_mode = INTERFACE_MODE_MAPPING[interface_mode]

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class="fvTenant",
            aci_rn="tn-{0}".format(tenant),
            module_object=tenant,
            target_filter={"name": tenant},
        ),
        subclass_1=dict(
            aci_class="fvAp",
            aci_rn="ap-{0}".format(ap),
            module_object=ap,
            target_filter={"name": ap},
        ),
        subclass_2=dict(
            aci_class="fvAEPg",
            aci_rn="epg-{0}".format(epg),
            module_object=epg,
            target_filter={"name": epg},
        ),
        subclass_3=dict(
            aci_class="fvRsPathAtt",
            aci_rn="rspathAtt-[{0}]".format(static_path),
            module_object=static_path,
            target_filter=path_target_filter,
        ),
    )

    aci.get_existing()

    if state == "present":
        aci.payload(
            aci_class="fvRsPathAtt",
            class_config=dict(
                descr=description,
                encap=encap_id,
                primaryEncap=primary_encap_id,
                instrImedcy=deploy_immediacy,
                mode=interface_mode,
                tDn=static_path,
            ),
        )

        aci.get_diff(aci_class="fvRsPathAtt")

        aci.post_config()

    elif state == "absent":
        aci.delete_config()

    aci.exit_json()


# 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 main():
    argument_spec = aci_argument_spec()
    argument_spec.update(
        tenant=dict(
            type="str", aliases=["tenant_name"]
        ),  # Not required for querying all objects
        ap=dict(
            type="str", aliases=["app_profile", "app_profile_name"]
        ),  # Not required for querying all objects
        epg=dict(
            type="str", aliases=["epg_name"]
        ),  # Not required for querying all objects
        description=dict(type="str", aliases=["descr"]),
        encap_id=dict(type="int", aliases=["vlan", "vlan_id"]),
        primary_encap_id=dict(type="int", aliases=["primary_vlan", "primary_vlan_id"]),
        deploy_immediacy=dict(type="str", choices=["immediate", "lazy"]),
        interface_mode=dict(
            type="str",
            choices=[
                "802.1p",
                "access",
                "native",
                "regular",
                "tagged",
                "trunk",
                "untagged",
            ],
            aliases=["interface_mode_name", "mode"],
        ),
        interface_type=dict(
            type="str",
            default="switch_port",
            choices=["fex", "port_channel", "switch_port", "vpc"],
        ),
        pod_id=dict(
            type="int", aliases=["pod", "pod_number"]
        ),  # Not required for querying all objects
        leafs=dict(type="list", aliases=["leaves", "nodes", "paths", "switches"]),
        interface=dict(type="str"),
        extpaths=dict(type="int"),
        state=dict(
            type="str", default="present", choices=["absent", "present", "query"]
        ),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=[
            ["interface_type", "fex", ["extpaths"]],
            [
                "state",
                "absent",
                ["ap", "epg", "interface", "leafs", "pod_id", "tenant"],
            ],
            [
                "state",
                "present",
                ["ap", "encap_id", "epg", "interface", "leafs", "pod_id", "tenant"],
            ],
        ],
    )

    tenant = module.params["tenant"]
    ap = module.params["ap"]
    epg = module.params["epg"]
    description = module.params["description"]
    encap_id = module.params["encap_id"]
    primary_encap_id = module.params["primary_encap_id"]
    deploy_immediacy = module.params["deploy_immediacy"]
    interface_mode = module.params["interface_mode"]
    interface_type = module.params["interface_type"]
    pod_id = module.params["pod_id"]
    leafs = module.params["leafs"]
    if leafs is not None:
        # Process leafs, and support dash-delimited leafs
        leafs = []
        for leaf in module.params["leafs"]:
            # Users are likely to use integers for leaf IDs, which would raise an exception when using the join method
            leafs.extend(str(leaf).split("-"))
        if len(leafs) == 1:
            if interface_type != "vpc":
                leafs = leafs[0]
            else:
                module.fail_json(msg='A interface_type of "vpc" requires 2 leafs')
        elif len(leafs) == 2:
            if interface_type == "vpc":
                leafs = "-".join(leafs)
            else:
                module.fail_json(
                    msg='The interface_types "switch_port", "port_channel", and "fex" \
                    do not support using multiple leafs for a single binding'
                )
        else:
            module.fail_json(
                msg='The "leafs" parameter must not have more than 2 entries'
            )
    interface = module.params["interface"]
    extpaths = module.params["extpaths"]
    state = module.params["state"]
    static_path = ""

    if encap_id is not None:
        if encap_id in range(1, 4097):
            encap_id = "vlan-{0}".format(encap_id)
        else:
            module.fail_json(msg="Valid VLAN assigments are from 1 to 4096")

    if primary_encap_id is not None:
        if primary_encap_id in range(1, 4097):
            primary_encap_id = "vlan-{0}".format(primary_encap_id)
        else:
            module.fail_json(msg="Valid VLAN assigments are from 1 to 4096")

    INTERFACE_MODE_MAPPING = {
        "802.1p": "native",
        "access": "untagged",
        "native": "native",
        "regular": "regular",
        "tagged": "regular",
        "trunk": "regular",
        "untagged": "untagged",
    }

    INTERFACE_TYPE_MAPPING = dict(
        fex="topology/pod-{0}/paths-{1}/extpaths-{2}/pathep-[eth{3}]".format(
            pod_id, leafs, extpaths, interface
        ),
        port_channel="topology/pod-{0}/paths-{1}/pathep-[{2}]".format(
            pod_id, leafs, interface
        ),
        switch_port="topology/pod-{0}/paths-{1}/pathep-[eth{2}]".format(
            pod_id, leafs, interface
        ),
        vpc="topology/pod-{0}/protpaths-{1}/pathep-[{2}]".format(
            pod_id, leafs, interface
        ),
    )

    static_path = INTERFACE_TYPE_MAPPING[interface_type]

    path_target_filter = {}
    if (
        pod_id is not None
        and leafs is not None
        and interface is not None
        and (interface_type != "fex" or extpaths is not None)
    ):
        path_target_filter = {"tDn": static_path}

    if interface_mode is not None:
        interface_mode = INTERFACE_MODE_MAPPING[interface_mode]

    aci = ACIModule(module)
    aci.construct_url(
        root_class=dict(
            aci_class="fvTenant",
            aci_rn="tn-{0}".format(tenant),
            module_object=tenant,
            target_filter={"name": tenant},
        ),
        subclass_1=dict(
            aci_class="fvAp",
            aci_rn="ap-{0}".format(ap),
            module_object=ap,
            target_filter={"name": ap},
        ),
        subclass_2=dict(
            aci_class="fvAEPg",
            aci_rn="epg-{0}".format(epg),
            module_object=epg,
            target_filter={"name": epg},
        ),
        subclass_3=dict(
            aci_class="fvRsPathAtt",
            aci_rn="rspathAtt-[{0}]".format(static_path),
            module_object=static_path,
            target_filter=path_target_filter,
        ),
    )

    aci.get_existing()

    if state == "present":
        aci.payload(
            aci_class="fvRsPathAtt",
            class_config=dict(
                descr=description,
                encap=encap_id,
                primaryEncap=primary_encap_id,
                instrImedcy=deploy_immediacy,
                mode=interface_mode,
                tDn=static_path,
            ),
        )

        aci.get_diff(aci_class="fvRsPathAtt")

        aci.post_config()

    elif state == "absent":
        aci.delete_config()

    aci.exit_json()