1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Allow jinja2 evals that produce lists to be used in with_items. Ideally want something a bit less heavy

than the eval here.
This commit is contained in:
Michael DeHaan 2013-04-10 15:22:08 -04:00
parent e8085ad40c
commit fecfbf9226
2 changed files with 23 additions and 4 deletions

View file

@ -336,18 +336,22 @@ class Runner(object):
inject.update(host_variables) inject.update(host_variables)
inject.update(self.module_vars) inject.update(self.module_vars)
inject.update(self.setup_cache[host]) inject.update(self.setup_cache[host])
inject['hostvars'] = HostVars(self.setup_cache, self.inventory)
inject['hostvars'] = HostVars(self.setup_cache, self.inventory)
inject['group_names'] = host_variables.get('group_names', []) inject['group_names'] = host_variables.get('group_names', [])
inject['groups'] = self.inventory.groups_list() inject['groups'] = self.inventory.groups_list()
inject['vars'] = self.module_vars inject['vars'] = self.module_vars
inject['environment'] = self.environment inject['environment'] = self.environment
if self.inventory.basedir() is not None: if self.inventory.basedir() is not None:
inject['inventory_dir'] = self.inventory.basedir() inject['inventory_dir'] = self.inventory.basedir()
# allow with_foo to work in playbooks... # allow with_foo to work in playbooks...
items = None items = None
items_plugin = self.module_vars.get('items_lookup_plugin', None) items_plugin = self.module_vars.get('items_lookup_plugin', None)
if items_plugin is not None and items_plugin in utils.plugins.lookup_loader: if items_plugin is not None and items_plugin in utils.plugins.lookup_loader:
items_terms = self.module_vars.get('items_lookup_terms', '') items_terms = self.module_vars.get('items_lookup_terms', '')
items_terms = utils.template(self.basedir, items_terms, inject) items_terms = utils.template(self.basedir, items_terms, inject)
items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=self.basedir).run(items_terms, inject=inject) items = utils.plugins.lookup_loader.get(items_plugin, runner=self, basedir=self.basedir).run(items_terms, inject=inject)
@ -364,8 +368,10 @@ class Runner(object):
if items is None: if items is None:
return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=self.complex_args) return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=self.complex_args)
elif len(items) > 0: elif len(items) > 0:
# executing using with_items, so make multiple calls # executing using with_items, so make multiple calls
# TODO: refactor # TODO: refactor
aggregrate = {} aggregrate = {}
all_comm_ok = True all_comm_ok = True
all_changed = False all_changed = False
@ -373,7 +379,14 @@ class Runner(object):
results = [] results = []
for x in items: for x in items:
inject['item'] = x inject['item'] = x
result = self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=self.complex_args) result = self._executor_internal_inner(
host,
self.module_name,
self.module_args,
inject,
port,
complex_args=self.complex_args
)
results.append(result.result) results.append(result.result)
if result.comm_ok == False: if result.comm_ok == False:
all_comm_ok = False all_comm_ok = False

View file

@ -31,5 +31,11 @@ class LookupModule(object):
def run(self, terms, **kwargs): def run(self, terms, **kwargs):
if isinstance(terms, basestring): if isinstance(terms, basestring):
if '{' or '[' in terms:
# Jinja2-ified list needs to be converted back to a real type
# TODO: something a bit less heavy than eval
terms = eval(terms)
terms = [ terms ] terms = [ terms ]
return flatten(terms) return flatten(terms)