Add custom globals to the environment, and not per template (#69278)

* Add custom globals to the environment, and not per template

* Add changelog fragment
pull/59737/head
Matt Martz 4 years ago committed by GitHub
parent 9667f221a5
commit bfff95393c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
minor_changes:
- Templating - Add globals to the jinja2 environment at ``Templar``
instantiation, instead of customizing the template object.
Only customize the template object, to disable lookups.
(https://github.com/ansible/ansible/pull/69278)

@ -572,6 +572,15 @@ class Templar:
loader=FileSystemLoader(self._basedir),
)
# jinja2 global is inconsistent across versions, this normalizes them
self.environment.globals['dict'] = dict
# Custom globals
self.environment.globals['lookup'] = self._lookup
self.environment.globals['query'] = self.environment.globals['q'] = self._query_lookup
self.environment.globals['now'] = self._now_datetime
self.environment.globals['finalize'] = self._finalize
# the current rendering context under which the templar class is working
self.cur_context = None
@ -997,18 +1006,8 @@ class Templar:
else:
return data
# jinja2 global is inconsistent across versions, this normalizes them
t.globals['dict'] = dict
if disable_lookups:
t.globals['query'] = t.globals['q'] = t.globals['lookup'] = self._fail_lookup
else:
t.globals['lookup'] = self._lookup
t.globals['query'] = t.globals['q'] = self._query_lookup
t.globals['now'] = self._now_datetime
t.globals['finalize'] = self._finalize
jvars = AnsibleJ2Vars(self, t.globals)

Loading…
Cancel
Save