Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow a variable in with_items #459

Merged
merged 2 commits into from
Jun 14, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 12 additions & 17 deletions lib/ansible/playbook/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,31 @@ def __init__(self, playbook, ds):
def _load_tasks(self, ds, keyname):
''' handle task and handler include statements '''

items = ds.get(keyname, [])
tasks = ds.get(keyname, [])
results = []
for x in items:
for x in tasks:
task_vars = self.vars.copy()
if 'include' in x:
task_vars = self.vars.copy()
tokens = shlex.split(x['include'])
for t in tokens[1:]:
(k,v) = t.split("=", 1)
task_vars[k]=v
include_file = tokens[0]
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:
items = x.get('with_items', None)
data = [x]
else:
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 = self.vars.copy()
mv = task_vars.copy()
mv['item'] = item
results.append(Task(self,x,module_vars=mv))
else:
raise Exception("unexpected task type")
results.append(Task(self,y,module_vars=mv))
return results

# *************************************************
Expand Down
10 changes: 8 additions & 2 deletions lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -223,6 +223,12 @@ def varLookup(name, vars):
_KEYCRE = re.compile(r"\$(?P<complex>\{){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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_varLookup_list(self):
}
}

res = ansible.utils.varLookup('data.who', vars)
res = ansible.utils._varLookup('data.who', vars)

assert sorted(res) == sorted(vars['data']['who'])

Expand Down