# Buggy code
def apply_key_map(key_map, table):
    new_dict = {}
    for key in table:
        new_key = key_map.get(key)
        if new_key:
            new_dict[new_key] = table.get(key)

    return new_dict


# Traceback (AttributeError)
$ansible-playbook -i /etc/ansible/hosts ../ap/test_nxos_bgp.yaml -vvvv
No config file found; using defaults
Loading callback plugin default of type stdout, v2.0 from /Users/mwiebe/Projects/nxos_ansible/fix_ansible/lib/ansible/plugins/callback/__init__.pyc

PLAYBOOK: test_nxos_bgp.yaml ********************************************************************************************************************************************************************
1 plays in ../ap/test_nxos_bgp.yaml

PLAY [nxos] *************************************************************************************************************************************************************************************
META: ran handlers

TASK [Configure a simple ASN] *******************************************************************************************************************************************************************
task path: /Users/mwiebe/Projects/nxos_ansible/ap/test_nxos_bgp.yaml:7
<n9k> connection transport is cli
<n9k> socket_path: /Users/mwiebe/.ansible/pc/30512dd245
open_shell() returned 0 ok
Using module file /Users/mwiebe/Projects/nxos_ansible/fix_ansible/lib/ansible/modules/network/nxos/nxos_bgp.py
<n9k> ESTABLISH LOCAL CONNECTION FOR USER: mwiebe
<n9k> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<n9k> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890 `" &amp;&amp; echo ansible-tmp-1493066974.43-188762941701890="` echo /Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890 `" ) &amp;&amp; sleep 0'
<n9k> PUT /var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/tmpX30gv1 TO /Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890/nxos_bgp.py
<n9k> EXEC /bin/sh -c 'chmod u+x /Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890/ /Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890/nxos_bgp.py &amp;&amp; sleep 0'
<n9k> EXEC /bin/sh -c '/Users/mwiebe/Virtualenvs/py2-ansible/bin/python /Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890/nxos_bgp.py; rm -rf "/Users/mwiebe/.ansible/tmp/ansible-tmp-1493066974.43-188762941701890/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py", line 874, in <module>
main()
File "/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py", line 824, in main
existing = invoke('get_existing', module, args)
File "/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py", line 468, in invoke
return func(*args, **kwargs)
File "/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py", line 569, in get_existing
config = netcfg.get_section(parents)
AttributeError: 'str' object has no attribute 'get_section'

fatal: [n9k]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py\", line 874, in <module>\n    main()\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py\", line 824, in main\n    existing = invoke('get_existing', module, args)\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py\", line 468, in invoke\n    return func(*args, **kwargs)\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_wTRkbb/ansible_module_nxos_bgp.py\", line 569, in get_existing\n    config = netcfg.get_section(parents)\nAttributeError: 'str' object has no attribute 'get_section'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}
to retry, use: --limit @/Users/mwiebe/Projects/nxos_ansible/ap/test_nxos_bgp.retry

PLAY RECAP **************************************************************************************************************************************************************************************
n9k          : ok=0    changed=0    unreachable=0    failed=1

# Fixed code
def apply_key_map(key_map, table):
    new_dict = {}
    for key, value in table.items():
        new_key = key_map.get(key)
        if new_key:
            new_dict[new_key] = value

    return new_dict