# Buggy code
def main():
    argument_spec = dict(
        vrf=dict(required=True, type="str"),
        safi=dict(required=True, type="str", choices=["unicast", "multicast"]),
        afi=dict(required=True, type="str", choices=["ipv4", "ipv6"]),
        route_target_both_auto_evpn=dict(required=False, type="bool"),
        m_facts=dict(required=False, default=False, type="bool"),
        state=dict(choices=["present", "absent"], default="present", required=False),
        include_defaults=dict(default=False),
        config=dict(),
        save=dict(type="bool", default=False),
    )

    argument_spec.update(nxos_argument_spec)

    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

    warnings = list()
    check_args(module, warnings)
    result = dict(changed=False, warnings=warnings)

    state = module.params["state"]
    args = PARAM_TO_COMMAND_KEYMAP.keys()
    existing = get_existing(module, args)
    proposed_args = dict(
        (k, v) for k, v in module.params.items() if v is not None and k in args
    )

    proposed = {}
    for key, value in proposed_args.items():
        if key != "interface":
            if str(value).lower() == "default":
                value = PARAM_TO_DEFAULT_KEYMAP.get(key)
                if value is None:
                    value = "default"
            if existing.get(key) != value:
                proposed[key] = value

    candidate = CustomNetworkConfig(indent=3)
    if state == "present":
        state_present(module, existing, proposed, candidate)
    elif state == "absent" and existing:
        state_absent(module, existing, proposed, candidate)

    if candidate:
        load_config(module, candidate)
        result["changed"] = True
        result["commands"] = candidate.items_text()

    else:
        result["commands"] = []
    module.exit_json(**result)


# Traceback (TypeError)
TASK [nxos_vrf_af : Configure vrf af] *****************************************************************************************************************
task path: /root/p2/Projects/nxos_ansible/p1_ansible/test/integration/targets/nxos_vrf_af/tests/nxapi/sanity.yaml:11
<n9k.example.com> connection transport is nxapi
Using module file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/modules/network/nxos/nxos_vrf_af.py
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<n9k.example.com> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806 `" &amp;&amp; echo ansible-tmp-1500584896.87-239558014882806="` echo /root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806 `" ) &amp;&amp; sleep 0'
<n9k.example.com> PUT /tmp/tmpSiIq_r TO /root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806/nxos_vrf_af.py
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806/ /root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806/nxos_vrf_af.py &amp;&amp; sleep 0'
<n9k.example.com> EXEC /bin/sh -c '/root/p2/bin/python /root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806/nxos_vrf_af.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1500584896.87-239558014882806/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_5nmRNH/ansible_module_nxos_vrf_af.py", line 251, in <module>
main()
File "/tmp/ansible_5nmRNH/ansible_module_nxos_vrf_af.py", line 241, in main
load_config(module, candidate)
File "/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py", line 397, in load_config
File "/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py", line 346, in load_config
File "/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py", line 262, in send_request
File "/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py", line 227, in _request_builder
TypeError: sequence item 0: expected string, CustomNetworkConfig found

fatal: [n9k.example.com]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_5nmRNH/ansible_module_nxos_vrf_af.py\", line 251, in <module>\n    main()\n  File \"/tmp/ansible_5nmRNH/ansible_module_nxos_vrf_af.py\", line 241, in main\n    load_config(module, candidate)\n  File \"/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py\", line 397, in load_config\n  File \"/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py\", line 346, in load_config\n  File \"/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py\", line 262, in send_request\n  File \"/tmp/ansible_5nmRNH/ansible_modlib.zip/ansible/module_utils/nxos.py\", line 227, in _request_builder\nTypeError: sequence item 0: expected string, CustomNetworkConfig found\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def main():
    argument_spec = dict(
        vrf=dict(required=True, type="str"),
        safi=dict(required=True, type="str", choices=["unicast", "multicast"]),
        afi=dict(required=True, type="str", choices=["ipv4", "ipv6"]),
        route_target_both_auto_evpn=dict(required=False, type="bool"),
        m_facts=dict(required=False, default=False, type="bool"),
        state=dict(choices=["present", "absent"], default="present", required=False),
    )

    argument_spec.update(nxos_argument_spec)

    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

    warnings = list()
    check_args(module, warnings)
    result = dict(changed=False, warnings=warnings)

    state = module.params["state"]
    args = PARAM_TO_COMMAND_KEYMAP.keys()
    existing = get_existing(module, args)
    proposed_args = dict(
        (k, v) for k, v in module.params.items() if v is not None and k in args
    )

    proposed = {}
    for key, value in proposed_args.items():
        if key != "interface":
            if str(value).lower() == "default":
                value = PARAM_TO_DEFAULT_KEYMAP.get(key)
                if value is None:
                    value = "default"
            if existing.get(key) != value:
                proposed[key] = value

    candidate = CustomNetworkConfig(indent=3)
    if state == "present":
        state_present(module, existing, proposed, candidate)
    elif state == "absent" and existing:
        state_absent(module, existing, proposed, candidate)

    if candidate:
        candidate = candidate.items_text()
        load_config(module, candidate)
        result["changed"] = True
        result["commands"] = candidate

    else:
        result["commands"] = []
    module.exit_json(**result)