# Buggy code
def main():
    argument_spec = vmware_argument_spec()
    argument_spec.update(
        state=dict(
            type="str",
            default="present",
            choices=[
                "absent",
                "poweredoff",
                "poweredon",
                "present",
                "rebootguest",
                "restarted",
                "shutdownguest",
                "suspended",
            ],
        ),
        template=dict(type="str", aliases=["template_src"]),
        is_template=dict(type="bool", default=False),
        annotation=dict(type="str", aliases=["notes"]),
        customvalues=dict(type="list", default=[]),
        name=dict(type="str"),
        name_match=dict(type="str", choices=["first", "last"], default="first"),
        uuid=dict(type="str"),
        folder=dict(type="str"),
        guest_id=dict(type="str"),
        disk=dict(type="list", default=[]),
        cdrom=dict(type="dict", default={}),
        hardware=dict(type="dict", default={}),
        force=dict(type="bool", default=False),
        datacenter=dict(type="str", default="ha-datacenter"),
        esxi_hostname=dict(type="str"),
        cluster=dict(type="str"),
        wait_for_ip_address=dict(type="bool", default=False),
        state_change_timeout=dict(type="int", default=0),
        snapshot_src=dict(type="str"),
        linked_clone=dict(type="bool", default=False),
        networks=dict(type="list", default=[]),
        resource_pool=dict(type="str"),
        customization=dict(type="dict", default={}, no_log=True),
        customization_spec=dict(type="str", default=None),
        vapp_properties=dict(type="list", default=[]),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        mutually_exclusive=[
            ["cluster", "esxi_hostname"],
        ],
        required_one_of=[
            ["name", "uuid"],
        ],
    )

    result = {"failed": False, "changed": False}

    pyv = PyVmomiHelper(module)

    # Check if the VM exists before continuing
    vm = pyv.get_vm()

    # VM already exists
    if vm:
        if module.params["state"] == "absent":
            # destroy it
            if module.check_mode:
                result.update(
                    vm_name=vm.name,
                    changed=True,
                    current_powerstate=vm.summary.runtime.powerState.lower(),
                    desired_operation="remove_vm",
                )
                module.exit_json(**result)
            if module.params["force"]:
                # has to be poweredoff first
                set_vm_power_state(
                    pyv.content, vm, "poweredoff", module.params["force"]
                )
            result = pyv.remove_vm(vm)
        elif module.params["state"] == "present":
            if module.check_mode:
                result.update(
                    vm_name=vm.name,
                    changed=True,
                    desired_operation="reconfigure_vm",
                )
                module.exit_json(**result)
            result = pyv.reconfigure_vm()
        elif module.params["state"] in [
            "poweredon",
            "poweredoff",
            "restarted",
            "suspended",
            "shutdownguest",
            "rebootguest",
        ]:
            if module.check_mode:
                result.update(
                    vm_name=vm.name,
                    changed=True,
                    current_powerstate=vm.summary.runtime.powerState.lower(),
                    desired_operation="set_vm_power_state",
                )
                module.exit_json(**result)
            # set powerstate
            tmp_result = set_vm_power_state(
                pyv.content,
                vm,
                module.params["state"],
                module.params["force"],
                module.params["state_change_timeout"],
            )
            if tmp_result["changed"]:
                result["changed"] = True
            if not tmp_result["failed"]:
                result["failed"] = False
            result["instance"] = tmp_result["instance"]
        else:
            # This should not happen
            raise AssertionError()
    # VM doesn't exist
    else:
        if module.params["state"] in [
            "poweredon",
            "poweredoff",
            "present",
            "restarted",
            "suspended",
        ]:
            if module.check_mode:
                result.update(
                    changed=True,
                    desired_operation="deploy_vm",
                )
                module.exit_json(**result)
            result = pyv.deploy_vm()
            if result["failed"]:
                module.fail_json(
                    msg="Failed to create a virtual machine : %s" % result["msg"]
                )

    if result["failed"]:
        module.fail_json(**result)
    else:
        module.exit_json(**result)


# 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 = vmware_argument_spec()
    argument_spec.update(
        state=dict(
            type="str",
            default="present",
            choices=[
                "absent",
                "poweredoff",
                "poweredon",
                "present",
                "rebootguest",
                "restarted",
                "shutdownguest",
                "suspended",
            ],
        ),
        template=dict(type="str", aliases=["template_src"]),
        is_template=dict(type="bool", default=False),
        annotation=dict(type="str", aliases=["notes"]),
        customvalues=dict(type="list", default=[]),
        name=dict(type="str"),
        name_match=dict(type="str", choices=["first", "last"], default="first"),
        uuid=dict(type="str"),
        folder=dict(type="str"),
        guest_id=dict(type="str"),
        disk=dict(type="list", default=[]),
        cdrom=dict(type="dict", default={}),
        hardware=dict(type="dict", default={}),
        force=dict(type="bool", default=False),
        datacenter=dict(type="str", default="ha-datacenter"),
        esxi_hostname=dict(type="str"),
        cluster=dict(type="str"),
        wait_for_ip_address=dict(type="bool", default=False),
        state_change_timeout=dict(type="int", default=0),
        snapshot_src=dict(type="str"),
        linked_clone=dict(type="bool", default=False),
        networks=dict(type="list", default=[]),
        resource_pool=dict(type="str"),
        customization=dict(type="dict", default={}, no_log=True),
        customization_spec=dict(type="str", default=None),
        vapp_properties=dict(type="list", default=[]),
        datastore=dict(type="str"),
    )

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=True,
        mutually_exclusive=[
            ["cluster", "esxi_hostname"],
        ],
        required_one_of=[
            ["name", "uuid"],
        ],
    )

    result = {"failed": False, "changed": False}

    pyv = PyVmomiHelper(module)

    # Check if the VM exists before continuing
    vm = pyv.get_vm()

    # VM already exists
    if vm:
        if module.params["state"] == "absent":
            # destroy it
            if module.check_mode:
                result.update(
                    vm_name=vm.name,
                    changed=True,
                    current_powerstate=vm.summary.runtime.powerState.lower(),
                    desired_operation="remove_vm",
                )
                module.exit_json(**result)
            if module.params["force"]:
                # has to be poweredoff first
                set_vm_power_state(
                    pyv.content, vm, "poweredoff", module.params["force"]
                )
            result = pyv.remove_vm(vm)
        elif module.params["state"] == "present":
            if module.check_mode:
                result.update(
                    vm_name=vm.name,
                    changed=True,
                    desired_operation="reconfigure_vm",
                )
                module.exit_json(**result)
            result = pyv.reconfigure_vm()
        elif module.params["state"] in [
            "poweredon",
            "poweredoff",
            "restarted",
            "suspended",
            "shutdownguest",
            "rebootguest",
        ]:
            if module.check_mode:
                result.update(
                    vm_name=vm.name,
                    changed=True,
                    current_powerstate=vm.summary.runtime.powerState.lower(),
                    desired_operation="set_vm_power_state",
                )
                module.exit_json(**result)
            # set powerstate
            tmp_result = set_vm_power_state(
                pyv.content,
                vm,
                module.params["state"],
                module.params["force"],
                module.params["state_change_timeout"],
            )
            if tmp_result["changed"]:
                result["changed"] = True
            if not tmp_result["failed"]:
                result["failed"] = False
            result["instance"] = tmp_result["instance"]
        else:
            # This should not happen
            raise AssertionError()
    # VM doesn't exist
    else:
        if module.params["state"] in [
            "poweredon",
            "poweredoff",
            "present",
            "restarted",
            "suspended",
        ]:
            if module.check_mode:
                result.update(
                    changed=True,
                    desired_operation="deploy_vm",
                )
                module.exit_json(**result)
            result = pyv.deploy_vm()
            if result["failed"]:
                module.fail_json(
                    msg="Failed to create a virtual machine : %s" % result["msg"]
                )

    if result["failed"]:
        module.fail_json(**result)
    else:
        module.exit_json(**result)