# Buggy code
def parse_level(line, dest):
    level_group = (
        "emergencies",
        "alerts",
        "critical",
        "errors",
        "warnings",
        "notifications",
        "informational",
        "debugging",
    )

    if dest == "hostnameprefix":
        level = "debugging"

    else:
        match = re.search(r"logging {} (\S+)".format(dest), line, re.M)
        if match:
            if match.group(1) in level_group:
                level = match.group(1)
            else:
                level = "debugging"
        else:
            level = "debugging"

    return level


# Traceback (AttributeError)
TASK [iosxr_logging : Set up host logging] ***********************************************************************************************************************************************************************************************************************************
task path: /Users/dnewswan/code/ansible/test/integration/targets/iosxr_logging/tests/cli/basic.yaml:2
<iosxr01> using connection plugin network_cli
<iosxr01> socket_path: /Users/dnewswan/.ansible/pc/c519def7ce
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/basic.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/iosxr.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/_text.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/parsing/__init__.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/pycompat24.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/six/__init__.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/six/_six.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/connection.py
Using module_utils file /Users/dnewswan/code/ansible/lib/ansible/module_utils/network_common.py
Using module file /Users/dnewswan/code/ansible/lib/ansible/modules/network/iosxr/iosxr_logging.py
<iosxr01> ESTABLISH LOCAL CONNECTION FOR USER: dnewswan
<iosxr01> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006 `" &amp;&amp; echo ansible-tmp-1500565779.7-23570874379006="` echo /Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006 `" ) &amp;&amp; sleep 0'
<iosxr01> PUT /var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/tmpRiQQSF TO /Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006/iosxr_logging.py
<iosxr01> EXEC /bin/sh -c 'chmod u+x /Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006/ /Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006/iosxr_logging.py &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c 'python /Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006/iosxr_logging.py; rm -rf "/Users/dnewswan/.ansible/tmp/ansible-tmp-1500565779.7-23570874379006/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_GQ9W8l/ansible_module_iosxr_logging.py", line 358, in <module>
main()
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_GQ9W8l/ansible_module_iosxr_logging.py", line 344, in main
have = map_config_to_obj(module)
File "/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_GQ9W8l/ansible_module_iosxr_logging.py", line 240, in map_config_to_obj
if match.group(1) in dest_group:
AttributeError: 'NoneType' object has no attribute 'group'

fatal: [iosxr01]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_GQ9W8l/ansible_module_iosxr_logging.py\", line 358, in <module>\n    main()\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_GQ9W8l/ansible_module_iosxr_logging.py\", line 344, in main\n    have = map_config_to_obj(module)\n  File \"/var/folders/ql/d9_087wj4cd10grvwzxmk4hc0000gn/T/ansible_GQ9W8l/ansible_module_iosxr_logging.py\", line 240, in map_config_to_obj\n    if match.group(1) in dest_group:\nAttributeError: 'NoneType' object has no attribute 'group'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def parse_level(line, dest):
    level_group = (
        "emergencies",
        "alerts",
        "critical",
        "errors",
        "warning",
        "notifications",
        "informational",
        "debugging",
    )

    if dest == "hostnameprefix":
        level = "debugging"

    else:
        match = re.search(r"logging {} (\S+)".format(dest), line, re.M)
        if match:
            if match.group(1) in level_group:
                level = match.group(1)
            else:
                level = "debugging"
        else:
            level = "debugging"

    return level