corrected host group ancestor management
This commit is contained in:
parent
273786d0bd
commit
3d5bc49a06
3 changed files with 28 additions and 12 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
||||
# 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):
|
||||
|
||||
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):
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue