# Buggy code
def get_existing(module, prefix, warnings):
    key_map = ["tag", "pref", "route_name", "next_hop"]
    netcfg = get_config(module)
    parents = "vrf context {0}".format(module.params["vrf"])
    prefix_to_regex = fix_prefix_to_regex(prefix)

    route_regex = (
        ".*ip\sroute\s{0}\s(?P<next_hop>\S+)(\sname\s(?P<route_name>\S+))?"
        "(\stag\s(?P<tag>\d+))?(\s(?P<pref>\d+)).*".format(prefix_to_regex)
    )

    if module.params["vrf"] == "default":
        config = str(netcfg)
    else:
        config = netcfg.get_section(parents)

    if config:
        try:
            match_route = re.match(route_regex, config, re.DOTALL)
            group_route = match_route.groupdict()

            for key in key_map:
                if key not in group_route:
                    group_route[key] = ""
            group_route["prefix"] = prefix
            group_route["vrf"] = module.params["vrf"]
        except (AttributeError, TypeError):
            group_route = {}
    else:
        group_route = {}
        msg = "VRF {0} didn't exist.".format(module.params["vrf"])
        if msg not in warnings:
            warnings.append(msg)

    return group_route


# Traceback (AttributeError)
ok: [s-srv1leaf2-2] => {
"purge_vnis": [
9999
]
}
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 589, in <module>
main()
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 533, in main
existing, interface_exist = invoke('get_existing', module, args)
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 316, in invoke
return func(*args, **kwargs)
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 377, in get_existing
temp_config = netcfg.get_section(parents)
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 192, in get_section
section = self.get_section_objects(path)
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 203, in get_section_objects
return self.expand_section(obj)
File "/tmp/ansible_yZvBEZ/ansible_module_nxos_vxlan_vtep_vni.py", line 175, in expand_section
if child in S:
File "/tmp/ansible_yZvBEZ/ansible_modlib.zip/ansible/module_utils/netcfg.py", line 47, in __eq__
AttributeError: 'str' object has no attribute 'line'

failed: [s-srv1leaf2-2] (item=9999) => {
"failed": true,
"invocation": {
"module_name": "nxos_vxlan_vtep_vni"
},
"item": 9999,

# Fixed code
def get_existing(module, prefix, warnings):
    key_map = ["tag", "pref", "route_name", "next_hop"]
    netcfg = CustomNetworkConfig(indent=2, contents=get_config(module))
    parents = "vrf context {0}".format(module.params["vrf"])
    prefix_to_regex = fix_prefix_to_regex(prefix)

    route_regex = (
        ".*ip\sroute\s{0}\s(?P<next_hop>\S+)(\sname\s(?P<route_name>\S+))?"
        "(\stag\s(?P<tag>\d+))?(\s(?P<pref>\d+)).*".format(prefix_to_regex)
    )

    if module.params["vrf"] == "default":
        config = str(netcfg)
    else:
        config = netcfg.get_section(parents)

    if config:
        try:
            match_route = re.match(route_regex, config, re.DOTALL)
            group_route = match_route.groupdict()

            for key in key_map:
                if key not in group_route:
                    group_route[key] = ""
            group_route["prefix"] = prefix
            group_route["vrf"] = module.params["vrf"]
        except (AttributeError, TypeError):
            group_route = {}
    else:
        group_route = {}
        msg = "VRF {0} didn't exist.".format(module.params["vrf"])
        if msg not in warnings:
            warnings.append(msg)

    return group_route