From 4b0480dd9d3836b0cd5efe8c860ed2886eccd294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20N=C3=A9ri?= Date: Sun, 13 May 2012 20:36:08 +0200 Subject: [PATCH 1/3] Set 'hostvars' before performing variable replacements --- lib/ansible/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 29b164de910..aacc9d5d6d1 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -257,8 +257,8 @@ def varReplace(raw, vars): def template(text, vars, setup_cache, no_engine=True): ''' run a text buffer through the templating engine ''' vars = vars.copy() - text = varReplace(unicode(text), vars) vars['hostvars'] = setup_cache + text = varReplace(unicode(text), vars) if no_engine: # used when processing include: directives so that Jinja is evaluated # in a later context when more variables are available From f8b17ef8d31592c57dd2f94f6652373d7d5ef879 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20N=C3=A9ri?= Date: Sun, 13 May 2012 20:42:11 +0200 Subject: [PATCH 2/3] For utils.template_from_file, change 'no_engine' default to True This now follows the other template functions, and implicitly also fixes that playbook handler includes were still processed by Jinja2. --- lib/ansible/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index aacc9d5d6d1..c6a7346fce1 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -273,7 +273,7 @@ def template(text, vars, setup_cache, no_engine=True): def double_template(text, vars, setup_cache): return template(template(text, vars, setup_cache), vars, setup_cache) -def template_from_file(path, vars, setup_cache, no_engine=False): +def template_from_file(path, vars, setup_cache, no_engine=True): ''' run a file through the templating engine ''' data = codecs.open(path, encoding="utf8").read() return template(data, vars, setup_cache, no_engine=no_engine) From e4991d1a61686794d8358113d9b9cd5cc61cb733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20N=C3=A9ri?= Date: Sun, 13 May 2012 20:57:01 +0200 Subject: [PATCH 3/3] In the template module, use utils.template_from_file instead of reimplementing it inline --- lib/ansible/runner.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index 730d8d7e419..3ebe089f503 100644 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -554,10 +554,9 @@ class Runner(object): copy_module = self._transfer_module(conn, tmp, 'copy') # template the source data locally - source_data = codecs.open(utils.path_dwim(self.basedir, source), encoding="utf8").read() - resultant = '' try: - resultant = utils.template(source_data, inject, self.setup_cache, no_engine=False) + resultant = utils.template_from_file(utils.path_dwim(self.basedir, source), + inject, self.setup_cache, no_engine=False) except Exception, e: return (host, False, dict(failed=True, msg=str(e)), '') xfered = self._transfer_str(conn, tmp, 'source', resultant)