From 6de8c270854ca0a846053e00ff18ad0bbe1d2a26 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Tue, 14 Aug 2012 19:48:33 -0400 Subject: [PATCH] Load parent groups when processing group_vars --- lib/ansible/inventory/__init__.py | 10 ++++++++++ lib/ansible/inventory/group.py | 4 +++- lib/ansible/playbook/play.py | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 019fb60d847..8af07a80ebe 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -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 diff --git a/lib/ansible/inventory/group.py b/lib/ansible/inventory/group.py index fe12f331c86..599b53c0a09 100644 --- a/lib/ansible/inventory/group.py +++ b/lib/ansible/inventory/group.py @@ -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): diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 4371155654a..550ed5571ce 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -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: