From ba1e373034223d277f9ef0be91b8a029e557408a Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Sat, 9 Jun 2012 21:59:22 +0200 Subject: [PATCH] Allow with_items to specify a variable --- lib/ansible/playbook/play.py | 2 ++ lib/ansible/utils.py | 10 ++++++++-- test/TestUtils.py | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 3f579747f40..40274c5108a 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -89,6 +89,8 @@ class Play(object): 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 diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index c507103d677..c06d3024d8a 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -201,7 +201,7 @@ def parse_json(data): _LISTRE = re.compile(r"(\w+)\[(\d+)\]") -def varLookup(name, vars): +def _varLookup(name, vars): ''' find the contents of a possibly complex variable in vars. ''' path = name.split('.') space = vars @@ -223,6 +223,12 @@ def varLookup(name, vars): _KEYCRE = re.compile(r"\$(?P\{){0,1}((?(complex)[\w\.\[\]]+|\w+))(?(complex)\})") # 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): '''Perform variable replacement of $vars @@ -245,7 +251,7 @@ def varReplace(raw, vars): # original) varname = m.group(2) - replacement = unicode(varLookup(varname, vars) or m.group()) + replacement = unicode(_varLookup(varname, vars) or m.group()) start, end = m.span() done.append(raw[:start]) # Keep stuff leading up to token diff --git a/test/TestUtils.py b/test/TestUtils.py index 7d03e17c47e..006a687e8b6 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -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'])