Replace lists with a comma-separated join

This commit is contained in:
Daniel Hokka Zakrisson 2012-09-30 11:41:44 +02:00
parent 13e8ef5f35
commit dfee6e19ca
2 changed files with 15 additions and 0 deletions

View file

@ -309,6 +309,8 @@ def varReplace(raw, vars, depth=0):
try: try:
replacement = _varLookup(m['path'], vars, depth) replacement = _varLookup(m['path'], vars, depth)
if isinstance(replacement, (list, tuple)):
replacement = ",".join(replacement)
if isinstance(replacement, (str, unicode)): if isinstance(replacement, (str, unicode)):
replacement = varReplace(replacement, vars, depth=depth + 1) replacement = varReplace(replacement, vars, depth=depth + 1)
except VarNotFoundException: except VarNotFoundException:

View file

@ -225,6 +225,19 @@ class TestUtils(unittest.TestCase):
res = ansible.utils.varReplace(template, vars) res = ansible.utils.varReplace(template, vars)
assert res == 'bar' assert res == 'bar'
def test_varReplace_list_join(self):
vars = {
'list': [
'foo',
'bar',
'baz',
],
}
template = 'yum pkg=${list} state=installed'
res = ansible.utils.varReplace(template, vars)
assert res == 'yum pkg=foo,bar,baz state=installed'
def test_template_varReplace_iterated(self): def test_template_varReplace_iterated(self):
template = 'hello $who' template = 'hello $who'
vars = { vars = {