corrected host group ancestor management

This commit is contained in:
Brian Coca 2017-03-15 11:52:55 -04:00
parent 273786d0bd
commit 3d5bc49a06
3 changed files with 28 additions and 12 deletions

View file

@ -96,6 +96,8 @@ class Group:
# isn't already a group with the same name # isn't already a group with the same name
if self.name not in [g.name for g in group.parent_groups]: if self.name not in [g.name for g in group.parent_groups]:
group.parent_groups.append(self) group.parent_groups.append(self)
for h in group.get_hosts():
h.populate_ancestors()
self.clear_hosts_cache() self.clear_hosts_cache()

View file

@ -106,27 +106,42 @@ class Host:
def set_gathered_facts(self, gathered): def set_gathered_facts(self, gathered):
self._gathered_facts = 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): 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): 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): def set_variable(self, key, value):
self.vars[key]=value self.vars[key]=value
def get_groups(self): def get_groups(self):
return self.groups
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()
def get_vars(self): def get_vars(self):

View file

@ -403,8 +403,7 @@ class VariableManager:
variables['ansible_playbook_python'] = sys.executable variables['ansible_playbook_python'] = sys.executable
if host: 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: if self._inventory:
variables['groups'] = self._inventory.get_group_dict() variables['groups'] = self._inventory.get_group_dict()