# Buggy code
def get_value(arg, config, module):
    custom = [
        "allowas_in_max",
        "additional_paths_send",
        "additional_paths_receive",
        "advertise_map_exist",
        "advertise_map_non_exist",
        "max_prefix_limit",
        "max_prefix_interval",
        "max_prefix_threshold",
        "max_prefix_warning",
        "next_hop_third_party",
        "soft_reconfiguration_in",
    ]
    command = PARAM_TO_COMMAND_KEYMAP[arg]
    has_command = re.search(r"\s+{0}\s*".format(command), config, re.M)
    has_command_val = command_val_re.search(
        r"(?:{0}\s)(?P<value>.*)$".format(command), config, re.M
    )

    if arg in custom:
        value = get_custom_value(arg, config, module)

    elif arg in BOOL_PARAMS:
        value = False
        if has_command:
            value = True

    elif command.split()[0] in ["filter-list", "prefix-list", "route-map"]:
        value = ""
        direction = arg.rsplit("_", 1)[1]
        if has_command_val:
            params = has_command_val.group("value").split()
            if params[-1] == direction:
                value = params[0]

    elif arg == "send_community":
        if has_command:
            value = "none"
            if has_command_val:
                value = has_command_val.group("value")

    else:
        value = ""

        if has_command_val:
            value = has_command_val.group("value")

    return value


# 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 get_value(arg, config, module):
    custom = [
        "allowas_in_max",
        "additional_paths_send",
        "additional_paths_receive",
        "advertise_map_exist",
        "advertise_map_non_exist",
        "max_prefix_limit",
        "max_prefix_interval",
        "max_prefix_threshold",
        "max_prefix_warning",
        "next_hop_third_party",
        "soft_reconfiguration_in",
    ]
    command = PARAM_TO_COMMAND_KEYMAP[arg]
    has_command = re.search(r"\s+{0}\s*".format(command), config, re.M)
    has_command_val = re.search(
        r"(?:{0}\s)(?P<value>.*)$".format(command), config, re.M
    )
    value = ""

    if arg in custom:
        value = get_custom_value(arg, config, module)

    elif arg in BOOL_PARAMS:
        value = False
        if has_command:
            value = True

    elif command.split()[0] in ["filter-list", "prefix-list", "route-map"]:
        direction = arg.rsplit("_", 1)[1]
        if has_command_val:
            params = has_command_val.group("value").split()
            if params[-1] == direction:
                value = params[0]

    elif arg == "send_community":
        if has_command:
            value = "none"
            if has_command_val:
                value = has_command_val.group("value")

    elif has_command_val:
        value = has_command_val.group("value")

    return value