# Buggy code
def get_network_command(existing, key, value):
    commands = []
    existing_networks = existing.get("networks", [])
    for inet in value:
        if not isinstance(inet, list):
            inet = [inet]
        if inet not in existing_networks:
            if len(inet) == 1:
                command = "{0} {1}".format(key, inet[0])
            elif len(inet) == 2:
                command = "{0} {1} route-map {2}".format(key, inet[0], inet[1])
            commands.append(command)
    for enet in existing_networks:
        if enet not in value:
            if len(enet) == 1:
                command = "no {0} {1}".format(key, enet[0])
            elif len(enet) == 2:
                command = "no {0} {1} route-map {2}".format(key, enet[0], enet[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_network_command(existing, key, value):
    commands = []
    existing_networks = existing.get("networks", [])
    for inet in value:
        if not isinstance(inet, list):
            inet = [inet]
        if inet not in existing_networks:
            if len(inet) == 1:
                command = "{0} {1}".format(key, inet[0])
            elif len(inet) == 2:
                command = "{0} {1} route-map {2}".format(key, inet[0], inet[1])
            if command:
                commands.append(command)
    for enet in existing_networks:
        if enet not in value:
            if len(enet) == 1:
                command = "no {0} {1}".format(key, enet[0])
            elif len(enet) == 2:
                command = "no {0} {1} route-map {2}".format(key, enet[0], enet[1])
            if command:
                commands.append(command)
    return commands