From 0f1706220b588226e5a6ab89d5c075f6c33d51ac Mon Sep 17 00:00:00 2001 From: Daniel Hokka Zakrisson Date: Wed, 19 Dec 2012 09:42:15 +0100 Subject: [PATCH] Add support for using the Jinja2 built-ins such as range --- lib/ansible/utils/template.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/ansible/utils/template.py b/lib/ansible/utils/template.py index 53ff0275fb..944efc7a9b 100644 --- a/lib/ansible/utils/template.py +++ b/lib/ansible/utils/template.py @@ -242,14 +242,18 @@ def template(basedir, text, vars, expand_lists=False): class _jinja2_vars(object): ''' helper class to template all variable content before jinja2 sees it ''' - def __init__(self, basedir, vars): + def __init__(self, basedir, vars, globals): self.basedir = basedir self.vars = vars + self.globals = globals def __contains__(self, k): - return k in self.vars + return k in self.vars or k in self.globals def __getitem__(self, varname): if varname not in self.vars: - raise KeyError("undefined variable: %s" % varname) + if varname in self.globals: + return self.globals[varname] + else: + raise KeyError("undefined variable: %s" % varname) var = self.vars[varname] # HostVars is special, return it as-is if isinstance(var, dict) and type(var) != dict: @@ -308,7 +312,7 @@ def template_from_file(basedir, path, vars): # This line performs deep Jinja2 magic that uses the _jinja2_vars object for vars # Ideally, this could use some API where setting shared=True and the object won't get # passed through dict(o), but I have not found that yet. - res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars), shared=True))) + res = jinja2.utils.concat(t.root_render_func(t.new_context(_jinja2_vars(basedir, vars, t.globals), shared=True))) if data.endswith('\n') and not res.endswith('\n'): res = res + '\n'