diff options
4 files changed, 19 insertions, 3 deletions
diff --git a/changelogs/fragments/74127-bad-filter-2.yml b/changelogs/fragments/74127-bad-filter-2.yml new file mode 100644 index 0000000000..39ef02b066 --- /dev/null +++ b/changelogs/fragments/74127-bad-filter-2.yml @@ -0,0 +1,5 @@ +bugfixes: +- Templating - Ensure we catch exceptions when getting ``.filters`` and + ``.tests`` attributes on their respective plugins and properly error, + instead of aborting which results in no filters being added to the + jinja2 environment diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index 3c9c476885..3c27587cb8 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -573,9 +573,8 @@ class JinjaPluginIntercept(MutableMapping): except Exception as e: raise TemplateSyntaxError(to_native(e), 0) - method_map = getattr(plugin_impl, self._method_map_name) - try: + method_map = getattr(plugin_impl, self._method_map_name) func_items = iteritems(method_map()) except Exception as e: display.warning( diff --git a/test/integration/targets/jinja_plugins/collections/ansible_collections/foo/bar/plugins/filter/bad_collection_filter2.py b/test/integration/targets/jinja_plugins/collections/ansible_collections/foo/bar/plugins/filter/bad_collection_filter2.py new file mode 100644 index 0000000000..96e726ac48 --- /dev/null +++ b/test/integration/targets/jinja_plugins/collections/ansible_collections/foo/bar/plugins/filter/bad_collection_filter2.py @@ -0,0 +1,10 @@ +# Copyright (c) 2021 Matt Martz <matt@sivel.net> +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +class FilterModule: + pass diff --git a/test/integration/targets/jinja_plugins/tasks/main.yml b/test/integration/targets/jinja_plugins/tasks/main.yml index 012ec9542c..013baecb2c 100644 --- a/test/integration/targets/jinja_plugins/tasks/main.yml +++ b/test/integration/targets/jinja_plugins/tasks/main.yml @@ -17,6 +17,8 @@ - | result.stderr|regex_findall('bad_test')|length == 2 - | - result.stderr|regex_findall('bad_collection_filter')|length == 2 + result.stderr|regex_findall('bad_collection_filter')|length == 3 + - | + result.stderr|regex_findall('bad_collection_filter2')|length == 1 - | result.stderr|regex_findall('bad_collection_test')|length == 2 |