From 39ba55e5ac547bf435c7b54f46ac416e01309ed6 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 15 Mar 2017 11:52:55 -0400 Subject: [PATCH] corrected host group ancestor management --- lib/ansible/inventory/group.py | 2 ++ lib/ansible/inventory/host.py | 35 ++++++++++++++++++++++++---------- lib/ansible/vars/__init__.py | 3 +-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/ansible/inventory/group.py b/lib/ansible/inventory/group.py index 73d432e545f..e7a96bb1ac3 100644 --- a/lib/ansible/inventory/group.py +++ b/lib/ansible/inventory/group.py @@ -96,6 +96,8 @@ class Group: # isn't already a group with the same name if self.name not in [g.name for g in group.parent_groups]: group.parent_groups.append(self) + for h in group.get_hosts(): + h.populate_ancestors() self.clear_hosts_cache() diff --git a/lib/ansible/inventory/host.py b/lib/ansible/inventory/host.py index 3902471e54a..3289d7e1628 100644 --- a/lib/ansible/inventory/host.py +++ b/lib/ansible/inventory/host.py @@ -106,27 +106,42 @@ class Host: def set_gathered_facts(self, gathered): self._gathered_facts = gathered + def populate_ancestors(self): + + # populate ancestors + for group in self.groups: + self.add_group(group) + def add_group(self, group): - self.groups.append(group) + # populate ancestors + for oldg in group.get_ancestors(): + if oldg not in self.groups: + self.add_group(oldg) + + if group not in self.groups: + self.groups.append(group) def remove_group(self, group): - self.groups.remove(group) + if group in self.groups: + self.groups.remove(group) + + # remove exclusive ancestors, xcept all! + for oldg in group.get_ancestors(): + if oldg.name != 'all': + for childg in self.groups: + if oldg in childg.get_ancestors(): + break + else: + self.remove_group(oldg) def set_variable(self, key, value): self.vars[key]=value def get_groups(self): - - groups = {} - for g in self.groups: - groups[g.name] = g - ancestors = g.get_ancestors() - for a in ancestors: - groups[a.name] = a - return groups.values() + return self.groups def get_vars(self): diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 8841384accd..5afdfd14ee6 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -403,8 +403,7 @@ class VariableManager: variables['ansible_playbook_python'] = sys.executable if host: - variables['group_names'] = sorted([group.name for group in host.get_groups() if group.name != 'all']) - + # host already provides some magic vars via host.get_vars() if self._inventory: variables['groups'] = self._inventory.get_group_dict()