# Buggy code
def state_present(module, existing, proposed, candidate):
    commands = list()
    proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed)
    existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing)

    for key, value in proposed_commands.items():
        if value is True:
            commands.append(key)

        elif value is False:
            commands.append("no {0}".format(key))

        elif value == "default":
            if existing_commands.get(key):
                existing_value = existing_commands.get(key)
                commands.append("no {0} {1}".format(key, existing_value))
        else:
            command = "{0} {1}".format(key, value.lower())
            commands.append(command)

    if commands:
        parents = ["vrf context {0}".format(module.params["vrf"])]
        parents.append(
            "address-family {0} {1}".format(module.params["afi"], module.params["safi"])
        )
        candidate.add(commands, parents=parents)


# 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 state_present(module, existing, proposed, candidate):
    commands = list()
    proposed_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, proposed)
    existing_commands = apply_key_map(PARAM_TO_COMMAND_KEYMAP, existing)

    for key, value in proposed_commands.items():
        if value is True:
            commands.append(key)

        elif value is False:
            commands.append("no {0}".format(key))

        elif value == "default":
            if existing_commands.get(key):
                existing_value = existing_commands.get(key)
                commands.append("no {0} {1}".format(key, existing_value))
        else:
            command = "{0} {1}".format(key, value.lower())
            commands.append(command)

    parents = ["vrf context {0}".format(module.params["vrf"])]
    parents.append(
        "address-family {0} {1}".format(module.params["afi"], module.params["safi"])
    )
    candidate.add(commands, parents=parents)