# Buggy code
def get_inject_map_command(existing, key, value):
    commands = []
    existing_maps = existing.get("inject_map", [])
    for maps in value:
        if not isinstance(maps, list):
            maps = [maps]
        if maps not in existing_maps:
            if len(maps) == 2:
                command = "inject-map {0} exist-map {1}".format(maps[0], maps[1])
            elif len(maps) == 3:
                command = "inject-map {0} exist-map {1} copy-attributes".format(
                    maps[0], maps[1]
                )
            commands.append(command)
    for emaps in existing_maps:
        if emaps not in value:
            if len(emaps) == 2:
                command = "no inject-map {0} exist-map {1}".format(emaps[0], emaps[1])
            elif len(emaps) == 3:
                command = "no inject-map {0} exist-map {1} copy-attributes".format(
                    emaps[0], emaps[1]
                )
            commands.append(command)
    return commands


# Traceback (UnboundLocalError)
TASK [leaf : print variable] ******************************************************************************************************************************
task path: /home/pete/ansible/playbooks/Lab/POD3/roles/leaf/tasks/main.yml:622
ok: [switch01] => {
"msg": [
[
"1.1.1.0/31",
""
],
[
"2.2.2.0/31",
""
],
[
"3.3.3.0/31",
""
],
[
"1.1.1.11/32",
""
],
[
"2.2.2.22/32",
""
]
]
}

===================================================================
===================================================================
TASK [leaf : Advertise Underlay Prefixes] ****************************************************************************************************
task path: /home/pete/ansible/playbooks/Lab/POD3/roles/leaf/tasks/main.yml:627
<switch01> connection transport is nxapi
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/network/nxos/nxos_bgp_af.py
<switch01> ESTABLISH LOCAL CONNECTION FOR USER: pete
<switch01> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<switch01> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639 `" &amp;&amp; echo ansible-tmp-1521146182.45-168292732118639="` echo /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639 `" ) &amp;&amp; sleep 0'
<switch01> PUT /home/pete/.ansible/tmp/ansible-local-14439mQqkUA/tmpfKCVRG TO /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639/nxos_bgp_af.py
<switch01> EXEC /bin/sh -c 'chmod u+x /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639/ /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639/nxos_bgp_af.py &amp;&amp; sleep 0'
<switch01> EXEC /bin/sh -c '/usr/bin/python /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639/nxos_bgp_af.py &amp;&amp; sleep 0'
<switch01> EXEC /bin/sh -c 'rm -f -r /home/pete/.ansible/tmp/ansible-tmp-1521146182.45-168292732118639/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py", line 825, in <module>
main()
File "/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py", line 809, in main
state_present(module, existing, proposed, candidate)
File "/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py", line 669, in state_present
network_commands = get_network_command(existing, key, value)
File "/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py", line 546, in get_network_command
commands.append(command)
UnboundLocalError: local variable 'command' referenced before assignment

fatal: [switch01]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py\", line 825, in <module>\n    main()\n  File \"/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py\", line 809, in main\n    state_present(module, existing, proposed, candidate)\n  File \"/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py\", line 669, in state_present\n    network_commands = get_network_command(existing, key, value)\n  File \"/tmp/ansible_pyrSvZ/ansible_module_nxos_bgp_af.py\", line 546, in get_network_command\n    commands.append(command)\nUnboundLocalError: local variable 'command' referenced before assignment\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def get_inject_map_command(existing, key, value):
    commands = []
    existing_maps = existing.get("inject_map", [])
    for maps in value:
        if not isinstance(maps, list):
            maps = [maps]
        if maps not in existing_maps:
            if len(maps) == 2:
                command = "inject-map {0} exist-map {1}".format(maps[0], maps[1])
            elif len(maps) == 3:
                command = "inject-map {0} exist-map {1} copy-attributes".format(
                    maps[0], maps[1]
                )
            if command:
                commands.append(command)
    for emaps in existing_maps:
        if emaps not in value:
            if len(emaps) == 2:
                command = "no inject-map {0} exist-map {1}".format(emaps[0], emaps[1])
            elif len(emaps) == 3:
                command = "no inject-map {0} exist-map {1} copy-attributes".format(
                    emaps[0], emaps[1]
                )
            if command:
                commands.append(command)
    return commands