Merge pull request #482 from dhozac/iterative-template

Iterative template
This commit is contained in:
Michael DeHaan 2012-06-16 11:13:11 -07:00
commit 09901f41ac
3 changed files with 21 additions and 4 deletions

View file

@ -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)

View file

@ -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 '''

View file

@ -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