# Buggy code
def get_available_features(feature, module):
    available_features = {}
    feature_regex = "(?P<feature>\S+)\s+\d+\s+(?P<state>.*)"
    command = "show feature"

    command = {"command": command, "output": "text"}

    body = run_commands(module, [command])
    split_body = body[0].splitlines()

    for line in split_body:
        try:
            match_feature = re.match(feature_regex, line, re.DOTALL)
            feature_group = match_feature.groupdict()
            feature = feature_group["feature"]
            state = feature_group["state"]
        except AttributeError:
            feature = ""
            state = ""

        if feature and state:
            if "enabled" in state:
                state = "enabled"

            if feature not in available_features:
                available_features[feature] = state
            else:
                if available_features[feature] == "disabled" and state == "enabled":
                    available_features[feature] = state

    return available_features


# Traceback (AttributeError)
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/var/folders/pz/zc91xtc54w99yt0h33t6gq9m0000gp/T/ansible_rtyOeQ/ansible_module_nxos_feature.py", line 481, in <module>
main()
File "/var/folders/pz/zc91xtc54w99yt0h33t6gq9m0000gp/T/ansible_rtyOeQ/ansible_module_nxos_feature.py", line 443, in main
available_features = get_available_features(feature, module)
File "/var/folders/pz/zc91xtc54w99yt0h33t6gq9m0000gp/T/ansible_rtyOeQ/ansible_module_nxos_feature.py", line 340, in get_available_features
split_body = body[0].splitlines()
AttributeError: 'dict' object has no attribute 'splitlines'

fatal: [10.56.140.14]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_name": "nxos_feature"
},
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/pz/zc91xtc54w99yt0h33t6gq9m0000gp/T/ansible_rtyOeQ/ansible_module_nxos_feature.py\", line 481, in <module>\n    main()\n  File \"/var/folders/pz/zc91xtc54w99yt0h33t6gq9m0000gp/T/ansible_rtyOeQ/ansible_module_nxos_feature.py\", line 443, in main\n    available_features = get_available_features(feature, module)\n  File \"/var/folders/pz/zc91xtc54w99yt0h33t6gq9m0000gp/T/ansible_rtyOeQ/ansible_module_nxos_feature.py\", line 340, in get_available_features\n    split_body = body[0].splitlines()\nAttributeError: 'dict' object has no attribute 'splitlines'\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}

# Fixed code
def get_available_features(feature, module):
    available_features = {}
    feature_regex = "(?P<feature>\S+)\s+\d+\s+(?P<state>.*)"
    command = "show feature"

    command = {"command": command, "output": "text"}

    try:
        body = run_commands(module, [command])[0]
        split_body = body.splitlines()
    except (KeyError, AttributeError, IndexError):
        return {}

    for line in split_body:
        try:
            match_feature = re.match(feature_regex, line, re.DOTALL)
            feature_group = match_feature.groupdict()
            feature = feature_group["feature"]
            state = feature_group["state"]
        except AttributeError:
            feature = ""
            state = ""

        if feature and state:
            if "enabled" in state:
                state = "enabled"

            if feature not in available_features:
                available_features[feature] = state
            else:
                if available_features[feature] == "disabled" and state == "enabled":
                    available_features[feature] = state

    return available_features