plugin_filter: check for type error (#46664)

* Parsing plugin filter may raise TypeError, gracefully handle this exception
and let user know about the syntax error in plugin filter file.

* Test for plugin_filtering

Fixes: #46658

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
(cherry picked from commit b32b4111b2)
This commit is contained in:
Abhijeet Kasurde 2018-10-30 18:25:02 +05:30 committed by Toshio Kuratomi
parent 44088ef64d
commit 1293076d7b
5 changed files with 26 additions and 4 deletions

View file

@ -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.

View file

@ -574,10 +574,9 @@ class Jinja2Loader(PluginLoader):
def _load_plugin_filter(): def _load_plugin_filter():
filters = defaultdict(frozenset) filters = defaultdict(frozenset)
user_set = False
if C.PLUGIN_FILTERS_CFG is None: if C.PLUGIN_FILTERS_CFG is None:
filter_cfg = '/etc/ansible/plugin_filters.yml' filter_cfg = '/etc/ansible/plugin_filters.yml'
user_set = False
else: else:
filter_cfg = C.PLUGIN_FILTERS_CFG filter_cfg = C.PLUGIN_FILTERS_CFG
user_set = True user_set = True
@ -605,11 +604,17 @@ def _load_plugin_filter():
if version == u'1.0': if version == u'1.0':
# Modules and action plugins share the same blacklist since the difference between the # Modules and action plugins share the same blacklist since the difference between the
# two isn't visible to the users # 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'] filters['ansible.plugins.action'] = filters['ansible.modules']
else: else:
display.warning(u'The plugin filter file, {0} was a version not recognized by this' 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: else:
if user_set: if user_set:
display.warning(u'The plugin filter file, {0} does not exist.' display.warning(u'The plugin filter file, {0} does not exist.'

View file

@ -0,0 +1,3 @@
[defaults]
retry_files_enabled = False
plugin_filters_cfg = ./no_blacklist_module.yml

View file

@ -0,0 +1,3 @@
---
filter_version: 1.0
module_blacklist:

View file

@ -21,6 +21,15 @@ if test $? != 0 ; then
exit 1 exit 1
fi 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 # Check that with these modules filtered out, all of these modules fail to be found
# #