From df3c4849e9f1ecbeabafca059c46e43d2cc66126 Mon Sep 17 00:00:00 2001 From: Stoned Elipot Date: Sun, 28 Apr 2013 15:20:43 +0200 Subject: [PATCH] Also load Jinja2 extensions when processing template from string --- lib/ansible/utils/template.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 88837cc08ea..927fdcd5187 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -49,6 +49,23 @@ def _get_filters(): return Globals.FILTERS +def _get_extensions(): + ''' return jinja2 extensions to load ''' + + ''' + if some extensions are set via jinja_extensions in ansible.cfg, we try + to load them with the jinja environment + ''' + jinja_exts = [] + if C.DEFAULT_JINJA2_EXTENSIONS: + ''' + Let's make sure the configuration directive doesn't contain spaces + and split extensions in an array + ''' + jinja_exts = C.DEFAULT_JINJA2_EXTENSIONS.replace(" ", "").split(',') + + return jinja_exts + class Flags: LEGACY_TEMPLATE_WARNING = False @@ -375,22 +392,10 @@ def template_from_file(basedir, path, vars): realpath = utils.path_dwim(basedir, path) loader=jinja2.FileSystemLoader([basedir,os.path.dirname(realpath)]) - ''' - if some extensions are set via jinja_extensions in ansible.cfg, we try - to load them with the jinja environment - ''' - jinja_exts = [] - if C.DEFAULT_JINJA2_EXTENSIONS: - ''' - Let's make sure the configuration directive doesn't contain spaces - and split extensions in an array - ''' - jinja_exts = C.DEFAULT_JINJA2_EXTENSIONS.replace(" ", "").split(',') - def my_lookup(*args, **kwargs): return lookup(*args, basedir=basedir, **kwargs) - environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=jinja_exts) + environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=_get_extensions()) environment.filters.update(_get_filters()) environment.globals['lookup'] = my_lookup @@ -445,12 +450,12 @@ def template_from_file(basedir, path, vars): return template(basedir, res, vars) def template_from_string(basedir, data, vars): - ''' run a file through the (Jinja2) templating engine ''' + ''' run a string through the (Jinja2) templating engine ''' try: if type(data) == str: data = unicode(data, 'utf-8') - environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined) + environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined, extensions=_get_extensions()) environment.filters.update(_get_filters()) environment.template_class = J2Template