From 20fc6a29d30897fc2dfa569065e1f8c8944f0b37 Mon Sep 17 00:00:00 2001 From: Timothy Appnel Date: Fri, 7 Sep 2012 18:07:52 -0400 Subject: [PATCH 1/2] Made groups.groupname and group_names variables accessible in playbooks. --- lib/ansible/inventory/__init__.py | 14 +++++++++++++- lib/ansible/runner/__init__.py | 8 +++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 6e83064f83f..ca0099b9cdf 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -35,7 +35,7 @@ class Inventory(object): """ __slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset', '_is_script', - 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache' ] + 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list' ] def __init__(self, host_list=C.DEFAULT_HOST_LIST): @@ -49,6 +49,7 @@ class Inventory(object): self._vars_per_host = {} self._vars_per_group = {} self._hosts_cache = {} + self._groups_list = {} # the inventory object holds a list of groups self.groups = [] @@ -213,6 +214,17 @@ class Inventory(object): continue return results + def groups_list(self): + if not self._groups_list: + groups = {} + for g in self.groups: + groups[g.name] = [h.name for h in g.get_hosts()] + ancestors = g.get_ancestors() + for a in ancestors: + groups[a.name] = [h.name for h in a.get_hosts()] + self._groups_list = groups + return self._groups_list + def get_groups(self): return self.groups diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index efb06e9892e..40737918762 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -250,17 +250,15 @@ class Runner(object): inject.update(self.module_vars) inject.update(self.setup_cache[host]) inject['hostvars'] = self.setup_cache + inject['group_names'] = host_variables.get('group_names', []) + inject['groups'] = self.inventory.groups_list() # allow with_items to work in playbooks... # apt and yum are converted into a single call, others run in a loop items = self.module_vars.get('items', []) if isinstance(items, basestring) and items.startswith("$"): - items = items.replace("$","") - if items in inject: - items = inject[items] - else: - raise errors.AnsibleError("unbound variable in with_items: %s" % items) + items = utils.varLookup(items, inject) if type(items) != list: raise errors.AnsibleError("with_items only takes a list: %s" % items) From 9d5a79f5864d18f0d876a264c9571c09a63a8c97 Mon Sep 17 00:00:00 2001 From: Timothy Appnel Date: Fri, 7 Sep 2012 18:07:52 -0400 Subject: [PATCH 2/2] Made groups.groupname and group_names variables accessible in playbooks. Also modified code that feeds the groups data structure to templates so that it resolves groups inside of groups to hostnames. --- lib/ansible/runner/__init__.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 40737918762..8f983b87aa2 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -319,11 +319,7 @@ class Runner(object): # 'hostvars' variable contains variables for each host name # ... and is set elsewhere # 'inventory_hostname' is also set elsewhere - group_hosts = {} - for g in self.inventory.groups: - group_hosts[g.name] = [ h.name for h in g.hosts ] - inject['groups'] = group_hosts - + inject['groups'] = self.inventory.groups_list() # allow module args to work as a dictionary # though it is usually a string new_args = ""