Load parent groups when processing group_vars

pull/878/head
Michael DeHaan 12 years ago
parent ded0c61750
commit 6de8c27085

@ -200,6 +200,16 @@ class Inventory(object):
hosts[host.name] = host
return sorted(hosts.values(), key=lambda x: x.name)
def groups_for_host(self, host):
results = []
groups = self.get_groups()
for group in groups:
for hostn in group.get_hosts():
if host == hostn.name:
results.append(group)
continue
return results
def get_groups(self):
return self.groups

@ -18,10 +18,11 @@
class Group(object):
''' a group of ansible hosts '''
__slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups' ]
__slots__ = [ 'name', 'hosts', 'vars', 'child_groups', 'parent_groups', 'depth' ]
def __init__(self, name=None):
self.depth = 0
self.name = name
self.hosts = []
self.vars = {}
@ -35,6 +36,7 @@ class Group(object):
if self == group:
raise Exception("can't add group to itself")
self.child_groups.append(group)
group.depth = group.depth + 1
group.parent_groups.append(self)
def add_host(self, host):

@ -226,7 +226,8 @@ class Play(object):
if (host is not None):
inventory = self.playbook.inventory
hostrec = inventory.get_host(host)
groups = [ g.name for g in hostrec.groups ]
groupz = sorted(inventory.groups_for_host(host), key=lambda g: g.depth)
groups = [ g.name for g in groupz ]
basedir = inventory.basedir()
if basedir is not None:
for x in groups:

Loading…
Cancel
Save