diff options
author | Felix Fontein <felix@fontein.de> | 2022-10-06 15:46:42 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-06 08:46:42 -0500 |
commit | fea62e755327c4fded5014afa8c36afb490f5744 (patch) | |
tree | 0abb4e5bd1142f577348c6228927cb5e68a49897 /lib/ansible/template | |
parent | 288dab3214f0241f12069565734a8211c415ffb6 (diff) | |
download | ansible-fea62e755327c4fded5014afa8c36afb490f5744.tar.gz |
Do not crash templating when filter/test name is not a valid Ansible plugin name (#78913) (#79026)
* Do not crash templating when filter/test name is not a valid Ansible plugin name.
* Store and re-raise KeyError if there was one.
Co-authored-by: s-hertel <19572925+s-hertel@users.noreply.github.com>
(cherry picked from commit 6d0aeac1e166842f2833f4fb64c727cc7f818118)
Diffstat (limited to 'lib/ansible/template')
-rw-r--r-- | lib/ansible/template/__init__.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 32e6854735..1498d3f813 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -423,12 +423,13 @@ class JinjaPluginIntercept(MutableMapping): if not isinstance(key, string_types): raise ValueError('key must be a string, got %s instead' % type(key)) + original_exc = None if key not in self._loaded_builtins: plugin = None try: plugin = self._pluginloader.get(key) except (AnsibleError, KeyError) as e: - raise TemplateSyntaxError('Could not load "%s": %s' % (key, to_native(e)), 0) + original_exc = e except Exception as e: display.vvvv('Unexpected plugin load (%s) exception: %s' % (key, to_native(e))) raise e @@ -439,8 +440,11 @@ class JinjaPluginIntercept(MutableMapping): self._delegatee[key] = plugin.j2_function self._loaded_builtins.add(key) - # let it trigger keyerror if we could not find ours or jinja2 one - func = self._delegatee[key] + # raise template syntax error if we could not find ours or jinja2 one + try: + func = self._delegatee[key] + except KeyError as e: + raise TemplateSyntaxError('Could not load "%s": %s' % (key, to_native(original_exc or e)), 0) # if i do have func and it is a filter, it nees wrapping if self._pluginloader.type == 'filter': |