diff options
author | James Cammarata <jimi@sngx.net> | 2017-01-19 23:13:09 -0600 |
---|---|---|
committer | James Cammarata <jimi@sngx.net> | 2017-01-19 23:13:09 -0600 |
commit | 757a05ae0a0c39b8a33e3cb2aaa9dd9b964828f1 (patch) | |
tree | ba83e88b1803bd5e4136376ce580e4d0ba6f5337 | |
parent | 05879d331ae23b9c4a77b70425c3f8e48ab2e8ea (diff) | |
download | ansible-issue_20063_jinja2_context_29.tar.gz |
Don't restrict local jinja2 variables to those that start with l_issue_20063_jinja2_context_29
Per a change in jinja2 2.9, local variables no longer are prefixed
with l_, so this updates AnsibleJ2Vars to pull in all locals (while
excluding some) regardless of name.
Fixes #20063
-rw-r--r-- | lib/ansible/template/__init__.py | 15 | ||||
-rw-r--r-- | lib/ansible/template/vars.py | 7 |
2 files changed, 16 insertions, 6 deletions
diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 7e0c559707..e29a8bf1f0 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -30,7 +30,7 @@ from numbers import Number from jinja2 import Environment from jinja2.loaders import FileSystemLoader from jinja2.exceptions import TemplateSyntaxError, UndefinedError -from jinja2.utils import concat as j2_concat +from jinja2.utils import concat as j2_concat, missing from jinja2.runtime import Context, StrictUndefined from ansible import constants as C from ansible.compat.six import string_types, text_type @@ -154,15 +154,22 @@ class AnsibleContext(Context): return True return False + def _update_unsafe(self, val): + if val is not None and not self.unsafe and self._is_unsafe(val): + self.unsafe = True + def resolve(self, key): ''' The intercepted resolve(), which uses the helper above to set the internal flag whenever an unsafe variable value is returned. ''' val = super(AnsibleContext, self).resolve(key) - if val is not None and not self.unsafe: - if self._is_unsafe(val): - self.unsafe = True + self._update_unsafe(val) + return val + + def resolve_or_missing(self, key): + val = super(AnsibleContext, self).resolve_or_missing(key) + self._update_unsafe(val) return val class AnsibleEnvironment(Environment): diff --git a/lib/ansible/template/vars.py b/lib/ansible/template/vars.py index 0e9b99e884..fc6140c297 100644 --- a/lib/ansible/template/vars.py +++ b/lib/ansible/template/vars.py @@ -50,8 +50,11 @@ class AnsibleJ2Vars: self._locals = dict() if isinstance(locals, dict): for key, val in iteritems(locals): - if key[:2] == 'l_' and val is not missing: - self._locals[key[2:]] = val + if val is not missing: + if key[:2] == 'l_': + self._locals[key[2:]] = val + elif key not in ('context', 'environment', 'template'): + self._locals[key] = val def __contains__(self, k): if k in self._templar._available_variables: |