Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterative template #482

Merged
merged 2 commits into from
Jun 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ansible/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ def _executor_internal(self, host):
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)
Expand Down
12 changes: 9 additions & 3 deletions lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 '''
Expand Down
11 changes: 11 additions & 0 deletions test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ def test_template_unicode(self):

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

Expand Down