mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #1159 from dhozac/include-with_items
Allow task includes to work with with_items
This commit is contained in:
commit
e756ee3741
3 changed files with 42 additions and 23 deletions
|
@ -101,23 +101,29 @@ class Play(object):
|
||||||
tasks = ds.get(keyname, [])
|
tasks = ds.get(keyname, [])
|
||||||
results = []
|
results = []
|
||||||
for x in tasks:
|
for x in tasks:
|
||||||
task_vars = self.vars.copy()
|
|
||||||
if 'include' in x:
|
if 'include' in x:
|
||||||
|
task_vars = self.vars.copy()
|
||||||
tokens = shlex.split(x['include'])
|
tokens = shlex.split(x['include'])
|
||||||
for t in tokens[1:]:
|
if 'with_items' in x:
|
||||||
(k,v) = t.split("=", 1)
|
items = utils.varReplaceWithItems(self.basedir, x['with_items'], task_vars)
|
||||||
task_vars[k] = utils.template(self.basedir, v, task_vars)
|
else:
|
||||||
include_file = utils.template(self.basedir, tokens[0], task_vars)
|
items = ['']
|
||||||
data = utils.parse_yaml_from_file(utils.path_dwim(self.basedir, include_file))
|
for item in items:
|
||||||
|
mv = task_vars.copy()
|
||||||
|
mv['item'] = item
|
||||||
|
for t in tokens[1:]:
|
||||||
|
(k,v) = t.split("=", 1)
|
||||||
|
mv[k] = utils.varReplaceWithItems(self.basedir, v, mv)
|
||||||
|
include_file = utils.template(self.basedir, tokens[0], mv)
|
||||||
|
data = utils.parse_yaml_from_file(utils.path_dwim(self.basedir, include_file))
|
||||||
|
for y in data:
|
||||||
|
results.append(Task(self,y,module_vars=mv))
|
||||||
elif type(x) == dict:
|
elif type(x) == dict:
|
||||||
data = [x]
|
task_vars = self.vars.copy()
|
||||||
|
results.append(Task(self,x,module_vars=task_vars))
|
||||||
else:
|
else:
|
||||||
raise Exception("unexpected task type")
|
raise Exception("unexpected task type")
|
||||||
|
|
||||||
for y in data:
|
|
||||||
mv = task_vars.copy()
|
|
||||||
results.append(Task(self,y,module_vars=mv))
|
|
||||||
|
|
||||||
for x in results:
|
for x in results:
|
||||||
if self.tags is not None:
|
if self.tags is not None:
|
||||||
x.tags.extend(self.tags)
|
x.tags.extend(self.tags)
|
||||||
|
|
|
@ -261,7 +261,7 @@ class Runner(object):
|
||||||
|
|
||||||
items = self.module_vars.get('items', [])
|
items = self.module_vars.get('items', [])
|
||||||
if isinstance(items, basestring) and items.startswith("$"):
|
if isinstance(items, basestring) and items.startswith("$"):
|
||||||
items = utils.varLookup(items, inject)
|
items = utils.varReplaceWithItems(self.basedir, items, inject)
|
||||||
if type(items) != list:
|
if type(items) != list:
|
||||||
raise errors.AnsibleError("with_items only takes a list: %s" % items)
|
raise errors.AnsibleError("with_items only takes a list: %s" % items)
|
||||||
|
|
||||||
|
|
|
@ -278,17 +278,6 @@ def _varFind(text):
|
||||||
path.append(text[part_start[0]:var_end])
|
path.append(text[part_start[0]:var_end])
|
||||||
return {'path': path, 'start': start, 'end': end}
|
return {'path': path, 'start': start, 'end': end}
|
||||||
|
|
||||||
def varLookup(varname, vars):
|
|
||||||
''' helper function used by with_items '''
|
|
||||||
|
|
||||||
m = _varFind(varname)
|
|
||||||
if not m:
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
return _varLookup(m['path'], vars)
|
|
||||||
except VarNotFoundException:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def varReplace(raw, vars, depth=0):
|
def varReplace(raw, vars, depth=0):
|
||||||
''' Perform variable replacement of $variables in string raw using vars dictionary '''
|
''' Perform variable replacement of $variables in string raw using vars dictionary '''
|
||||||
# this code originally from yum
|
# this code originally from yum
|
||||||
|
@ -360,6 +349,30 @@ def varReplaceFilesAndPipes(basedir, raw):
|
||||||
|
|
||||||
return ''.join(done)
|
return ''.join(done)
|
||||||
|
|
||||||
|
def varReplaceWithItems(basedir, varname, vars):
|
||||||
|
''' helper function used by with_items '''
|
||||||
|
|
||||||
|
if isinstance(varname, basestring):
|
||||||
|
m = _varFind(varname)
|
||||||
|
if not m:
|
||||||
|
return varname
|
||||||
|
if m['start'] == 0 and m['end'] == len(varname):
|
||||||
|
try:
|
||||||
|
return varReplaceWithItems(basedir, _varLookup(m['path'], vars), vars)
|
||||||
|
except VarNotFoundException:
|
||||||
|
return varname
|
||||||
|
else:
|
||||||
|
return template(basedir, varname, vars)
|
||||||
|
elif isinstance(varname, (list, tuple)):
|
||||||
|
return [varReplaceWithItems(basedir, v, vars) for v in varname]
|
||||||
|
elif isinstance(varname, dict):
|
||||||
|
d = {}
|
||||||
|
for (k, v) in varname.iteritems():
|
||||||
|
d[k] = varReplaceWithItems(basedir, v, vars)
|
||||||
|
return d
|
||||||
|
else:
|
||||||
|
raise Exception("invalid with_items type")
|
||||||
|
|
||||||
|
|
||||||
def template(basedir, text, vars):
|
def template(basedir, text, vars):
|
||||||
''' run a text buffer through the templating engine until it no longer changes '''
|
''' run a text buffer through the templating engine until it no longer changes '''
|
||||||
|
|
Loading…
Reference in a new issue