From 8bb8314d10942681cf40779e1cfc4f789e3e7e32 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Tue, 17 Jul 2012 20:08:45 -0400 Subject: [PATCH] Inject vars need to pushed further up to make with_items work as desired. --- lib/ansible/playbook/task.py | 4 ---- lib/ansible/runner/__init__.py | 30 +++++++++++++++++++----------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/ansible/playbook/task.py b/lib/ansible/playbook/task.py index 1a8323fa94..c1ff016c68 100644 --- a/lib/ansible/playbook/task.py +++ b/lib/ansible/playbook/task.py @@ -95,10 +95,6 @@ class Task(object): # process with_items so it can be used by Runner code if self.with_items is None: self.with_items = [ ] - elif isinstance(self.with_items, basestring): - self.with_items = utils.varLookup(self.with_items, self.module_vars) - if type(self.with_items) != list: - raise errors.AnsibleError("with_items must be a list, got: %s" % self.with_items) self.module_vars['items'] = self.with_items # tags allow certain parts of a playbook to be run without running the whole playbook diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index cf600c335c..27889c1ccc 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -548,9 +548,24 @@ class Runner(object): def _executor_internal(self, host): ''' executes any module one or more times ''' + host_variables = self.inventory.get_variables(host) + port = host_variables.get('ansible_ssh_port', self.remote_port) + inject = self.setup_cache[host].copy() + inject.update(host_variables) + inject.update(self.module_vars) + 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) + if type(items) != list: + raise errors.AnsibleError("with_items only takes a list: %s" % items) + if len(items) == 0: - return self._executor_internal_inner(host) + return self._executor_internal_inner(host, inject, port) else: # executing using with_items, so make multiple calls # TODO: refactor @@ -560,8 +575,8 @@ class Runner(object): all_failed = False results = [] for x in items: - self.module_vars['item'] = x - result = self._executor_internal_inner(host) + inject['item'] = x + result = self._executor_internal_inner(host, inject, port) results.append(result.result) if result.comm_ok == False: all_comm_ok = False @@ -582,16 +597,9 @@ class Runner(object): # ***************************************************** - def _executor_internal_inner(self, host): + def _executor_internal_inner(self, host, inject, port): ''' decides how to invoke a module ''' - host_variables = self.inventory.get_variables(host) - port = host_variables.get('ansible_ssh_port', self.remote_port) - - inject = self.setup_cache[host].copy() - inject.update(host_variables) - inject.update(self.module_vars) - # special non-user/non-fact variables: # 'groups' variable is a list of host name in each group # 'hostvars' variable contains variables for each host name