# Buggy code
def get_value(arg, config, module):
    custom = ["inject_map", "networks", "redistribute"]

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

    elif (
        arg.startswith("distance")
        or arg.startswith("dampening")
        or arg.startswith("table_map")
    ):
        value = get_custom_string_value(config, arg, module)

    elif arg in BOOL_PARAMS:
        command_re = re.compile(r"\s+{0}\s*".format(command), re.M)
        value = False

        if command_re.search(config):
            value = True

    else:
        command_val_re = re.compile(
            r"(?:{0}\s)(?P<value>.*)$".format(PARAM_TO_COMMAND_KEYMAP[arg]), re.M
        )
        value = ""

        has_command = command_val_re.search(config)
        if has_command:
            value = has_command.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):
    command = PARAM_TO_COMMAND_KEYMAP[arg]
    command_val_re = re.compile(r"(?:{0}\s)(?P<value>.*)$".format(command), re.M)
    has_command_val = command_val_re.search(config)

    if arg == "inject_map":
        inject_re = (
            r".*inject-map\s(?P<inject_map>\S+)\sexist-map\s(?P<exist_map>\S+)-*"
        )

        value = []
        match_inject = re.match(inject_re, config, re.DOTALL)
        if match_inject:
            inject_group = match_inject.groupdict()
            inject_map = inject_group["inject_map"]
            exist_map = inject_group["exist_map"]
            value.append(inject_map)
            value.append(exist_map)

            inject_map_command = "inject-map {0} exist-map {1} copy-attributes".format(
                inject_group["inject_map"], inject_group["exist_map"]
            )

            inject_re = re.compile(r"\s+{0}\s*$".format(inject_map_command), re.M)
            if inject_re.search(config):
                value.append("copy_attributes")

    elif arg in ["networks", "redistribute"]:
        value = []
        if has_command_val:
            value = has_command_val.group("value").split()

            if value:
                if len(value) == 3:
                    value.pop(1)
                elif arg == "redistribute" and len(value) == 4:
                    value = ["{0} {1}".format(value[0], value[1]), value[3]]

    elif command == "distance":
        distance_re = r".*distance\s(?P<d_ebgp>\w+)\s(?P<d_ibgp>\w+)\s(?P<d_local>\w+)"
        match_distance = re.match(distance_re, config, re.DOTALL)

        value = ""
        if match_distance:
            distance_group = match_distance.groupdict()

            if arg == "distance_ebgp":
                value = distance_group["d_ebgp"]
            elif arg == "distance_ibgp":
                value = distance_group["d_ibgp"]
            elif arg == "distance_local":
                value = distance_group["d_local"]

    elif command.split()[0] == "dampening":
        value = ""
        if arg == "dampen_igp_metric" or arg == "dampening_routemap":
            if command in config:
                value = has_command_val.group("value")
        else:
            dampening_re = r".*dampening\s(?P<half>\w+)\s(?P<reuse>\w+)\s(?P<suppress>\w+)\s(?P<max_suppress>\w+)"
            match_dampening = re.match(dampening_re, config, re.DOTALL)
            if match_dampening:
                dampening_group = match_dampening.groupdict()

                if arg == "dampening_half_time":
                    value = dampening_group["half"]
                elif arg == "dampening_reuse_time":
                    value = dampening_group["reuse"]
                elif arg == "dampening_suppress_time":
                    value = dampening_group["suppress"]
                elif arg == "dampening_max_suppress_time":
                    value = dampening_group["max_suppress"]

    elif arg == "table_map_filter":
        tmf_regex = re.compile(r"\s+table-map.*filter$", re.M)
        value = False
        if tmf_regex.search(config):
            value = True

    elif arg == "table_map":
        tm_regex = re.compile(r"(?:table-map\s)(?P<value>\S+)(\sfilter)?$", re.M)
        has_tablemap = tm_regex.search(config)
        value = ""
        if has_tablemap:
            value = has_tablemap.group("value")

    elif arg in BOOL_PARAMS:
        command_re = re.compile(r"\s+{0}\s*".format(command), re.M)
        value = False

        if command_re.search(config):
            value = True

    else:
        value = ""

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

    return value