Inject vars need to pushed further up to make with_items work as desired.

This commit is contained in:
Michael DeHaan 2012-07-17 20:08:45 -04:00
parent 4e74ab4856
commit 8bb8314d10
2 changed files with 19 additions and 15 deletions

View file

@ -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

View file

@ -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