diff --git a/lib/ansible/template.py b/lib/ansible/template.py index f88ab2b18ed..767266da04b 100644 --- a/lib/ansible/template.py +++ b/lib/ansible/template.py @@ -40,7 +40,7 @@ import pwd _LISTRE = re.compile(r"(\w+)\[(\d+)\]") -def _varFindLimitSpace(space, part, depth): +def _varFindLimitSpace(vars, space, part, depth): # TODO: comments @@ -101,7 +101,7 @@ def _varFind(text, vars, depth=0): pass elif is_complex and text[end] == '.': if brace_level == part_start[1]: - space = _varFindLimitSpace(space, text[part_start[0]:end], depth) + space = _varFindLimitSpace(vars, space, text[part_start[0]:end], depth) part_start = (end + 1, brace_level) else: break @@ -113,7 +113,7 @@ def _varFind(text, vars, depth=0): return None if var_end == part_start[0]: return None - space = _varFindLimitSpace(space, text[part_start[0]:var_end], depth) + space = _varFindLimitSpace(vars, space, text[part_start[0]:var_end], depth) return {'replacement': space, 'start': start, 'end': end} def varReplace(raw, vars, depth=0, expand_lists=False): diff --git a/test/TestUtils.py b/test/TestUtils.py index ca958d7aa33..5c1df13bcb3 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -238,6 +238,28 @@ class TestUtils(unittest.TestCase): res = ansible.utils.varReplace(template, vars) assert res == 'action $foo' + def test_varReplace_var_part(self): + vars = { + 'foo': { + 'bar': 'result', + }, + 'key': 'bar', + } + template = 'test ${foo.$key}' + res = ansible.utils.varReplace(template, vars) + assert res == 'test result' + + def test_varReplace_var_partial_part(self): + vars = { + 'foo': { + 'barbaz': 'result', + }, + 'key': 'bar', + } + template = 'test ${foo.${key}baz}' + res = ansible.utils.varReplace(template, vars) + assert res == 'test result' + def test_template_varReplace_iterated(self): template = 'hello $who' vars = {