From fb7716bdea1acc78f64e8efc164ba0f18d8db055 Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Sun, 17 Jun 2012 22:18:08 +0200 Subject: [PATCH 1/2] Create a Jinja2 environment allowing includes --- lib/ansible/runner/__init__.py | 3 +-- lib/ansible/utils.py | 30 ++++++++++++++---------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py index f9c91c4f41c..0f6ed655417 100644 --- a/lib/ansible/runner/__init__.py +++ b/lib/ansible/runner/__init__.py @@ -586,8 +586,7 @@ class Runner(object): # template the source data locally try: - resultant = utils.template_from_file(utils.path_dwim(self.basedir, source), - inject, self.setup_cache, no_engine=False) + resultant = utils.template_from_file(self.basedir, source, inject, self.setup_cache) except Exception, e: result = dict(failed=True, msg=str(e)) return ReturnData(host=conn.host, comm_ok=False, result=result) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index fefdb87df10..ee87cc72785 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -265,35 +265,33 @@ def varReplace(raw, vars): return ''.join(done) -def _template(text, vars, setup_cache=None, no_engine=True): +def _template(text, vars, setup_cache=None): ''' run a text buffer through the templating engine ''' vars = vars.copy() 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 - return text - else: - template = jinja2.Template(text) - res = template.render(vars) - if text.endswith('\n') and not res.endswith('\n'): - res = res + '\n' - return res + return text -def template(text, vars, setup_cache=None, no_engine=True): +def template(text, vars, setup_cache=None): ''' 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) + text = _template(text, vars, setup_cache) return text -def template_from_file(path, vars, setup_cache, no_engine=True): +def template_from_file(basedir, path, vars, setup_cache): ''' run a file through the templating engine ''' - data = codecs.open(path, encoding="utf8").read() - return template(data, vars, setup_cache, no_engine=no_engine) + environment = jinja2.Environment(loader=jinja2.FileSystemLoader(basedir), trim_blocks=False) + data = codecs.open(path_dwim(basedir, path), encoding="utf8").read() + template = environment.from_string(data) + vars = vars.copy() + vars['hostvars'] = setup_cache + res = template.render(vars) + if data.endswith('\n') and not res.endswith('\n'): + res = res + '\n' + return res def parse_yaml(data): return yaml.load(data) From 32422e94050ff07e13834908e89d332f1ea2ee6e Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Mon, 18 Jun 2012 00:12:42 +0200 Subject: [PATCH 2/2] Update tests to reflect new API --- test/TestUtils.py | 63 +++++++++++++++++++--------------------- test/template-basic | 1 + test/template-whitespace | 1 + 3 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 test/template-basic create mode 100644 test/template-whitespace diff --git a/test/TestUtils.py b/test/TestUtils.py index b83dcb0312d..84601c6f050 100644 --- a/test/TestUtils.py +++ b/test/TestUtils.py @@ -203,39 +203,6 @@ class TestUtils(unittest.TestCase): assert res == 'hello world' - ##################################### - ### Template function tests - - def test_template_basic(self): - template = 'hello {{ who }}' - vars = { - 'who': 'world', - } - - res = ansible.utils.template(template, vars, {}, no_engine=False) - - assert res == 'hello world' - - def test_template_whitespace(self): - template = 'hello {{ who }}\n' - vars = { - 'who': 'world', - } - - res = ansible.utils.template(template, vars, {}, no_engine=False) - - assert res == 'hello world\n' - - def test_template_unicode(self): - template = 'hello {{ who }}' - vars = { - 'who': u'wórld', - } - - res = ansible.utils.template(template, vars, {}, no_engine=False) - - assert res == u'hello wórld' - def test_template_varReplace_iterated(self): template = 'hello $who' vars = { @@ -247,6 +214,36 @@ class TestUtils(unittest.TestCase): assert res == u'hello oh great one' + ##################################### + ### Template function tests + + def test_template_basic(self): + vars = { + 'who': 'world', + } + + res = ansible.utils.template_from_file("test", "template-basic", vars, {}) + + assert res == 'hello world' + + def test_template_whitespace(self): + vars = { + 'who': 'world', + } + + res = ansible.utils.template_from_file("test", "template-whitespace", vars, {}) + + assert res == 'hello world\n' + + def test_template_unicode(self): + vars = { + 'who': u'wórld', + } + + res = ansible.utils.template_from_file("test", "template-basic", vars, {}) + + assert res == u'hello wórld' + ##################################### ### key-value parsing diff --git a/test/template-basic b/test/template-basic new file mode 100644 index 00000000000..9aa58b909c2 --- /dev/null +++ b/test/template-basic @@ -0,0 +1 @@ +hello {{ who }} \ No newline at end of file diff --git a/test/template-whitespace b/test/template-whitespace new file mode 100644 index 00000000000..795953837b8 --- /dev/null +++ b/test/template-whitespace @@ -0,0 +1 @@ +hello {{ who }}