Simplify group_by by removing BYPASS_HOST_LOOP from the action

Fixes #12825
pull/12835/head
James Cammarata 9 years ago
parent 0888d78b84
commit 1fa975d81a

@ -145,7 +145,7 @@ class ResultProcess(multiprocessing.Process):
self._send_result(('add_host', result_item)) self._send_result(('add_host', result_item))
elif 'add_group' in result_item: elif 'add_group' in result_item:
# this task added a new group (group_by module) # this task added a new group (group_by module)
self._send_result(('add_group', result._task)) self._send_result(('add_group', result._host, result_item))
elif 'ansible_facts' in result_item: elif 'ansible_facts' in result_item:
# if this task is registering facts, do that now # if this task is registering facts, do that now
item = result_item.get('item', None) item = result_item.get('item', None)

@ -23,7 +23,6 @@ class ActionModule(ActionBase):
''' Create inventory groups based on variables ''' ''' Create inventory groups based on variables '''
### We need to be able to modify the inventory ### We need to be able to modify the inventory
BYPASS_HOST_LOOP = True
TRANSFERS_FILES = False TRANSFERS_FILES = False
def run(self, tmp=None, task_vars=dict()): def run(self, tmp=None, task_vars=dict()):

@ -223,14 +223,15 @@ class StrategyBase:
ret_results.append(task_result) ret_results.append(task_result)
elif result[0] == 'add_host': elif result[0] == 'add_host':
task_result = result[1] result_item = result[1]
new_host_info = task_result.get('add_host', dict()) new_host_info = result_item.get('add_host', dict())
self._add_host(new_host_info) self._add_host(new_host_info)
elif result[0] == 'add_group': elif result[0] == 'add_group':
task = result[1] host = result[1]
self._add_group(task, iterator) result_item = result[2]
self._add_group(host, result_item)
elif result[0] == 'notify_handler': elif result[0] == 'notify_handler':
task_result = result[1] task_result = result[1]
@ -353,44 +354,35 @@ class StrategyBase:
# FIXME: is this still required? # FIXME: is this still required?
self._inventory.clear_pattern_cache() self._inventory.clear_pattern_cache()
def _add_group(self, task, iterator): def _add_group(self, host, result_item):
''' '''
Helper function to add a group (if it does not exist), and to assign the Helper function to add a group (if it does not exist), and to assign the
specified host to that group. specified host to that group.
''' '''
changed = False
# the host here is from the executor side, which means it was a # the host here is from the executor side, which means it was a
# serialized/cloned copy and we'll need to look up the proper # serialized/cloned copy and we'll need to look up the proper
# host object from the master inventory # host object from the master inventory
groups = {} real_host = self._inventory.get_host(host.name)
changed = False
for host in self._inventory.get_hosts(): group_name = result_item.get('add_group')
original_task = iterator.get_original_task(host, task) new_group = self._inventory.get_group(group_name)
all_vars = self._variable_manager.get_vars(loader=self._loader, play=iterator._play, host=host, task=original_task) if not new_group:
templar = Templar(loader=self._loader, variables=all_vars) # create the new group and add it to inventory
group_name = templar.template(original_task.args.get('key')) new_group = Group(name=group_name)
if task.evaluate_conditional(templar=templar, all_vars=all_vars): self._inventory.add_group(new_group)
if group_name not in groups: new_group.vars = self._inventory.get_group_vars(new_group)
groups[group_name] = []
groups[group_name].append(host) # and add the group to the proper hierarchy
allgroup = self._inventory.get_group('all')
for group_name, hosts in iteritems(groups): allgroup.add_child_group(new_group)
new_group = self._inventory.get_group(group_name) changed = True
if not new_group:
# create the new group and add it to inventory if group_name not in host.get_groups():
new_group = Group(name=group_name) new_group.add_host(real_host)
self._inventory.add_group(new_group) changed = True
new_group.vars = self._inventory.get_group_vars(new_group)
# and add the group to the proper hierarchy
allgroup = self._inventory.get_group('all')
allgroup.add_child_group(new_group)
changed = True
for host in hosts:
if group_name not in host.get_groups():
new_group.add_host(host)
changed = True
return changed return changed

Loading…
Cancel
Save