'Helper function to add a group (if it does not exist), and to assign the
specified host to that group.'

def _add_group(self, host, result_item):

    changed = False
    real_host = self._inventory.hosts[host.name]
    group_name = result_item.get('add_group')
    parent_group_names = result_item.get('parent_groups', [])
    for name in ([group_name] + parent_group_names):
        if (name not in self._inventory.groups):
            self._inventory.add_group(name)
            changed = True
    group = self._inventory.groups[group_name]
    for parent_group_name in parent_group_names:
        parent_group = self._inventory.groups[parent_group_name]
        parent_group.add_child_group(group)
    if (real_host.name not in group.get_hosts()):
        group.add_host(real_host)
        changed = True
    if (group_name not in host.get_groups()):
        real_host.add_group(group)
        changed = True
    if changed:
        self._inventory.clear_pattern_cache()
        self._inventory.reconcile_inventory()
    return changed
