diff --git a/changelogs/fragments/46658-plugin_filter-improve_error_handling.yaml b/changelogs/fragments/46658-plugin_filter-improve_error_handling.yaml new file mode 100644 index 0000000000..ed85f01660 --- /dev/null +++ b/changelogs/fragments/46658-plugin_filter-improve_error_handling.yaml @@ -0,0 +1,2 @@ +minor_changes: +- Parsing plugin filter may raise TypeError, gracefully handle this exception and let user know about the syntax error in plugin filter file. diff --git a/lib/ansible/plugins/loader.py b/lib/ansible/plugins/loader.py index 04f8f95440..a6927e18a3 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -574,10 +574,9 @@ class Jinja2Loader(PluginLoader): def _load_plugin_filter(): filters = defaultdict(frozenset) - + user_set = False if C.PLUGIN_FILTERS_CFG is None: filter_cfg = '/etc/ansible/plugin_filters.yml' - user_set = False else: filter_cfg = C.PLUGIN_FILTERS_CFG user_set = True @@ -605,11 +604,17 @@ def _load_plugin_filter(): if version == u'1.0': # Modules and action plugins share the same blacklist since the difference between the # two isn't visible to the users - filters['ansible.modules'] = frozenset(filter_data['module_blacklist']) + try: + filters['ansible.modules'] = frozenset(filter_data['module_blacklist']) + except TypeError: + display.warning(u'Unable to parse the plugin filter file {0} as' + u' module_blacklist is not a list.' + u' Skipping.'.format(filter_cfg)) + return filters filters['ansible.plugins.action'] = filters['ansible.modules'] else: display.warning(u'The plugin filter file, {0} was a version not recognized by this' - u' version of Ansible. Skipping.') + u' version of Ansible. Skipping.'.format(filter_cfg)) else: if user_set: display.warning(u'The plugin filter file, {0} does not exist.' diff --git a/test/integration/targets/plugin_filtering/no_blacklist_module.ini b/test/integration/targets/plugin_filtering/no_blacklist_module.ini new file mode 100644 index 0000000000..65b51d6712 --- /dev/null +++ b/test/integration/targets/plugin_filtering/no_blacklist_module.ini @@ -0,0 +1,3 @@ +[defaults] +retry_files_enabled = False +plugin_filters_cfg = ./no_blacklist_module.yml diff --git a/test/integration/targets/plugin_filtering/no_blacklist_module.yml b/test/integration/targets/plugin_filtering/no_blacklist_module.yml new file mode 100644 index 0000000000..52a55dff3c --- /dev/null +++ b/test/integration/targets/plugin_filtering/no_blacklist_module.yml @@ -0,0 +1,3 @@ +--- +filter_version: 1.0 +module_blacklist: diff --git a/test/integration/targets/plugin_filtering/runme.sh b/test/integration/targets/plugin_filtering/runme.sh index d1ef407879..aa0e2b0c2a 100755 --- a/test/integration/targets/plugin_filtering/runme.sh +++ b/test/integration/targets/plugin_filtering/runme.sh @@ -21,6 +21,15 @@ if test $? != 0 ; then exit 1 fi +# +# Check that if no modules are blacklisted then Ansible should not through traceback +# +ANSIBLE_CONFIG=no_blacklist_module.ini ansible-playbook tempfile.yml -i ../../inventory -vvv "$@" +if test $? != 0 ; then + echo "### Failed to run tempfile with no modules blacklisted" + exit 1 +fi + # # Check that with these modules filtered out, all of these modules fail to be found #