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

Merge pull request #459 from dhozac/with_items-variable

Allow a variable in with_items
This commit is contained in:
Michael DeHaan 2012-06-13 19:18:51 -07:00
commit bd893b765b
3 changed files with 23 additions and 22 deletions

View file

@ -70,36 +70,31 @@ class Play(object):
def _load_tasks(self, ds, keyname): def _load_tasks(self, ds, keyname):
''' handle task and handler include statements ''' ''' handle task and handler include statements '''
items = ds.get(keyname, []) tasks = ds.get(keyname, [])
results = [] results = []
for x in items: 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:]: for t in tokens[1:]:
(k,v) = t.split("=", 1) (k,v) = t.split("=", 1)
task_vars[k]=v task_vars[k]=v
include_file = tokens[0] include_file = tokens[0]
data = utils.parse_yaml_from_file(utils.path_dwim(self.playbook.basedir, include_file)) data = utils.parse_yaml_from_file(utils.path_dwim(self.playbook.basedir, include_file))
for y in data:
items = y.get('with_items',None)
if items is None:
items = [ '' ]
for item in items:
mv = self.vars.copy()
mv.update(task_vars)
mv['item'] = item
results.append(Task(self,y,module_vars=mv))
elif type(x) == dict: elif type(x) == dict:
items = x.get('with_items', None) data = [x]
if items is None:
items = [ '' ]
for item in items:
mv = self.vars.copy()
mv['item'] = item
results.append(Task(self,x,module_vars=mv))
else: else:
raise Exception("unexpected task type") raise Exception("unexpected task type")
for y in data:
items = y.get('with_items',None)
if items is None:
items = [ '' ]
elif isinstance(items, basestring):
items = utils.varLookup(items, task_vars)
for item in items:
mv = task_vars.copy()
mv['item'] = item
results.append(Task(self,y,module_vars=mv))
return results return results
# ************************************************* # *************************************************

View file

@ -201,7 +201,7 @@ def parse_json(data):
_LISTRE = re.compile(r"(\w+)\[(\d+)\]") _LISTRE = re.compile(r"(\w+)\[(\d+)\]")
def varLookup(name, vars): def _varLookup(name, vars):
''' find the contents of a possibly complex variable in vars. ''' ''' find the contents of a possibly complex variable in vars. '''
path = name.split('.') path = name.split('.')
space = vars space = vars
@ -223,6 +223,12 @@ def varLookup(name, vars):
_KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})") _KEYCRE = re.compile(r"\$(?P<complex>\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})")
# if { -> complex if complex, allow . and need trailing } # if { -> complex if complex, allow . and need trailing }
def varLookup(varname, vars):
m = _KEYCRE.search(varname)
if not m:
return None
return _varLookup(m.group(2), vars)
def varReplace(raw, vars): def varReplace(raw, vars):
'''Perform variable replacement of $vars '''Perform variable replacement of $vars
@ -245,7 +251,7 @@ def varReplace(raw, vars):
# original) # original)
varname = m.group(2) varname = m.group(2)
replacement = unicode(varLookup(varname, vars) or m.group()) replacement = unicode(_varLookup(varname, vars) or m.group())
start, end = m.span() start, end = m.span()
done.append(raw[:start]) # Keep stuff leading up to token done.append(raw[:start]) # Keep stuff leading up to token

View file

@ -17,7 +17,7 @@ class TestUtils(unittest.TestCase):
} }
} }
res = ansible.utils.varLookup('data.who', vars) res = ansible.utils._varLookup('data.who', vars)
assert sorted(res) == sorted(vars['data']['who']) assert sorted(res) == sorted(vars['data']['who'])