# Buggy code
def _parse_group(self, group, group_data):
    if isinstance(group_data, (MutableMapping, NoneType)):
        try:
            self.inventory.add_group(group)
        except AnsibleError as e:
            raise AnsibleParserError("Unable to add group %s: %s" % (group, to_text(e)))

        if group_data is not None:
            # make sure they are dicts
            for section in ["vars", "children", "hosts"]:
                if section in group_data:
                    # convert strings to dicts as these are allowed
                    if isinstance(group_data[section], string_types):
                        group_data[section] = {group_data[section]: None}

                    if not isinstance(group_data[section], (MutableMapping, NoneType)):
                        raise AnsibleParserError(
                            'Invalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.'
                            % (section, group, type(group_data[section]))
                        )

            for key in group_data:
                if key == "vars":
                    for var in group_data[key]:
                        self.inventory.set_variable(group, var, group_data[key][var])
                elif key == "children":
                    for subgroup in group_data[key]:
                        self._parse_group(subgroup, group_data[key][subgroup])
                        self.inventory.add_child(group, subgroup)

                elif key == "hosts":
                    for host_pattern in group_data[key]:
                        hosts, port = self._parse_host(host_pattern)
                        self._populate_host_vars(
                            hosts, group_data[key][host_pattern] or {}, group, port
                        )
                else:
                    self.display.warning(
                        'Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid'
                        % (key, group)
                    )

    else:
        self.display.warning(
            "Skipping '%s' as this is not a valid group definition" % group
        )


# Traceback (KeyError)
ansible-playbook -i ./environments/network/network-inventory ./playbooks/network/ios-aaa.yml --flush-cache -vvv
ansible-playbook 2.7.0
config file = /Users/tnielsen/dev/ansible/ansible.cfg
configured module search path = [u'/Users/tnielsen/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 2.7.15 (default, Oct  2 2018, 11:42:04) [GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.2)]
Using /Users/tnielsen/dev/ansible/ansible.cfg as config file
Parsed /Users/tnielsen/dev/ansible/environments/network/network-inventory inventory source with ini plugin
ERROR! Unexpected Exception, this is probably a bug: u'core.sthd.corp.yamww.com'
the full traceback was:

Traceback (most recent call last):
File "/usr/local/bin/ansible-playbook", line 118, in <module>
exit_code = cli.run()
File "/usr/local/lib/python2.7/site-packages/ansible/cli/playbook.py", line 116, in run
self._flush_cache(inventory, variable_manager)
File "/usr/local/lib/python2.7/site-packages/ansible/cli/playbook.py", line 196, in _flush_cache
variable_manager.clear_facts(hostname)
File "/usr/local/lib/python2.7/site-packages/ansible/vars/manager.py", line 613, in clear_facts
del self._fact_cache[hostname]
File "/usr/local/lib/python2.7/site-packages/ansible/plugins/cache/__init__.py", line 273, in __delitem__
self._plugin.delete(key)
File "/usr/local/lib/python2.7/site-packages/ansible/plugins/cache/redis.py", line 119, in delete
del self._cache[key]
KeyError: u'core.sthd.corp.yamww.com'

# Fixed code
def _parse_group(self, group, group_data):
    if isinstance(group_data, (MutableMapping, NoneType)):
        try:
            self.inventory.add_group(group)
        except AnsibleError as e:
            raise AnsibleParserError("Unable to add group %s: %s" % (group, to_text(e)))

        if group_data is not None:
            # make sure they are dicts
            for section in ["vars", "children", "hosts"]:
                if section in group_data:
                    # convert strings to dicts as these are allowed
                    if isinstance(group_data[section], string_types):
                        group_data[section] = {group_data[section]: None}

                    if not isinstance(group_data[section], (MutableMapping, NoneType)):
                        raise AnsibleParserError(
                            'Invalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.'
                            % (section, group, type(group_data[section]))
                        )

            for key in group_data:
                if not isinstance(group_data[key], (MutableMapping, NoneType)):
                    self.display.warning(
                        "Skipping key (%s) in group (%s) as it is not a mapping, it is a %s"
                        % (key, group, type(group_data[key]))
                    )
                    continue

                if isinstance(group_data[key], NoneType):
                    self.display.vvv(
                        "Skipping empty key (%s) in group (%s)" % (key, group)
                    )
                elif key == "vars":
                    for var in group_data[key]:
                        self.inventory.set_variable(group, var, group_data[key][var])
                elif key == "children":
                    for subgroup in group_data[key]:
                        self._parse_group(subgroup, group_data[key][subgroup])
                        self.inventory.add_child(group, subgroup)

                elif key == "hosts":
                    for host_pattern in group_data[key]:
                        hosts, port = self._parse_host(host_pattern)
                        self._populate_host_vars(
                            hosts, group_data[key][host_pattern] or {}, group, port
                        )
                else:
                    self.display.warning(
                        'Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid'
                        % (key, group)
                    )

    else:
        self.display.warning(
            "Skipping '%s' as this is not a valid group definition" % group
        )