Skip to content

Commit

Permalink
Only expand lists in templating inside of module action lines, to avo…
Browse files Browse the repository at this point in the history
…id breaking usage with with_items and "in" statements, etc
  • Loading branch information
mpdehaan committed Oct 17, 2012
1 parent d21d367 commit 25162c9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/ansible/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def _executor_internal_inner(self, host, module_name, module_args, inject, port,
return ReturnData(host=host, comm_ok=False, result=result)

module_name = utils.template(self.basedir, module_name, inject)
module_args = utils.template(self.basedir, module_args, inject)
module_args = utils.template(self.basedir, module_args, inject, expand_lists=True)

tmp = ''
if self.module_name != 'raw':
Expand Down
10 changes: 5 additions & 5 deletions lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _varFind(text):
path.append(text[part_start[0]:var_end])
return {'path': path, 'start': start, 'end': end}

def varReplace(raw, vars, depth=0):
def varReplace(raw, vars, depth=0, expand_lists=False):
''' Perform variable replacement of $variables in string raw using vars dictionary '''
# this code originally from yum

Expand All @@ -300,10 +300,10 @@ def varReplace(raw, vars, depth=0):

try:
replacement = _varLookup(m['path'], vars, depth)
if isinstance(replacement, (list, tuple)):
if expand_lists and isinstance(replacement, (list, tuple)):
replacement = ",".join(replacement)
if isinstance(replacement, (str, unicode)):
replacement = varReplace(replacement, vars, depth=depth + 1)
replacement = varReplace(replacement, vars, depth=depth+1, expand_lists=expand_lists)
except VarNotFoundException:
replacement = raw[m['start']:m['end']]

Expand Down Expand Up @@ -376,15 +376,15 @@ def varReplaceWithItems(basedir, varname, vars):
return varname


def template(basedir, text, vars):
def template(basedir, text, vars, expand_lists=False):
''' run a text buffer through the templating engine until it no longer changes '''

prev_text = ''
try:
text = text.decode('utf-8')
except UnicodeEncodeError:
pass # already unicode
text = varReplace(unicode(text), vars)
text = varReplace(unicode(text), vars, expand_lists=expand_lists)
text = varReplaceFilesAndPipes(basedir, text)
return text

Expand Down
2 changes: 1 addition & 1 deletion test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def test_varReplace_list_join(self):
}

template = 'yum pkg=${list} state=installed'
res = ansible.utils.varReplace(template, vars)
res = ansible.utils.varReplace(template, vars, expand_lists=True)
assert res == 'yum pkg=foo,bar,baz state=installed'

def test_template_varReplace_iterated(self):
Expand Down

0 comments on commit 25162c9

Please sign in to comment.