From 13b814a33c6ff35a5e22c8887a1e08196bb3fc66 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Sat, 9 Jun 2012 18:40:17 +0200 Subject: [PATCH 1/2] Run templating function until the text doesn't change This allows variables to contain other variables. --- lib/ansible/runner/__init__.py | 2 +- lib/ansible/utils.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index 6867699229..f9c91c4f41 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -648,7 +648,7 @@ class Runner(object): inject.update(host_variables) inject.update(self.module_vars) - conditional = utils.double_template(self.conditional, inject, self.setup_cache) + conditional = utils.template(self.conditional, inject, self.setup_cache) if not eval(conditional): result = utils.smjson(dict(skipped=True)) self.callbacks.on_skipped(host) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index a29f7ea254..fefdb87df1 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -265,7 +265,7 @@ def varReplace(raw, vars): return ''.join(done) -def template(text, vars, setup_cache=None, no_engine=True): +def _template(text, vars, setup_cache=None, no_engine=True): ''' run a text buffer through the templating engine ''' vars = vars.copy() vars['hostvars'] = setup_cache @@ -281,8 +281,14 @@ def template(text, vars, setup_cache=None, no_engine=True): res = res + '\n' return res -def double_template(text, vars, setup_cache): - return template(template(text, vars, setup_cache), vars, setup_cache) +def template(text, vars, setup_cache=None, no_engine=True): + ''' run a text buffer through the templating engine + until it no longer changes ''' + prev_text = '' + while prev_text != text: + prev_text = text + text = _template(text, vars, setup_cache, no_engine) + return text def template_from_file(path, vars, setup_cache, no_engine=True): ''' run a file through the templating engine ''' From 24d73b0fe6fdc4503fd7ef2a00db9894065d6a9c Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Sat, 16 Jun 2012 13:13:01 +0200 Subject: [PATCH 2/2] Add tests of iterative templating --- test/TestUtils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/TestUtils.py b/test/TestUtils.py index 006a687e8b..b83dcb0312 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -236,6 +236,17 @@ class TestUtils(unittest.TestCase): assert res == u'hello wórld' + def test_template_varReplace_iterated(self): + template = 'hello $who' + vars = { + 'who': 'oh great $person', + 'person': 'one', + } + + res = ansible.utils.template(template, vars) + + assert res == u'hello oh great one' + ##################################### ### key-value parsing