mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-24 03:11:24 -07:00
Implement plugin filtering
This commit is contained in:
parent
14c3b4d8e5
commit
340a7be7c3
22 changed files with 312 additions and 4 deletions
|
@ -17,8 +17,10 @@ import warnings
|
|||
from collections import defaultdict
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.plugins import get_plugin_class, MODULE_CACHE, PATH_CACHE, PLUGIN_PATH_CACHE
|
||||
from ansible.errors import AnsibleError
|
||||
from ansible.module_utils._text import to_text
|
||||
from ansible.parsing.utils.yaml import from_yaml
|
||||
from ansible.plugins import get_plugin_class, MODULE_CACHE, PATH_CACHE, PLUGIN_PATH_CACHE
|
||||
from ansible.utils.plugin_docs import get_docstring
|
||||
|
||||
try:
|
||||
|
@ -235,6 +237,10 @@ class PluginLoader:
|
|||
def find_plugin(self, name, mod_type='', ignore_deprecated=False, check_aliases=False):
|
||||
''' Find a plugin named name '''
|
||||
|
||||
global _PLUGIN_FILTERS
|
||||
if name in _PLUGIN_FILTERS[self.package]:
|
||||
return None
|
||||
|
||||
if mod_type:
|
||||
suffix = mod_type
|
||||
elif self.class_name:
|
||||
|
@ -405,6 +411,8 @@ class PluginLoader:
|
|||
def all(self, *args, **kwargs):
|
||||
''' instantiates all plugins with the same arguments '''
|
||||
|
||||
global _PLUGIN_FILTERS
|
||||
|
||||
path_only = kwargs.pop('path_only', False)
|
||||
class_only = kwargs.pop('class_only', False)
|
||||
all_matches = []
|
||||
|
@ -416,7 +424,7 @@ class PluginLoader:
|
|||
for path in sorted(all_matches, key=os.path.basename):
|
||||
name = os.path.basename(os.path.splitext(path)[0])
|
||||
|
||||
if '__init__' in name:
|
||||
if '__init__' in name or name in _PLUGIN_FILTERS[self.package]:
|
||||
continue
|
||||
|
||||
if path_only:
|
||||
|
@ -462,6 +470,63 @@ class PluginLoader:
|
|||
self._update_object(obj, name, path)
|
||||
yield obj
|
||||
|
||||
|
||||
def _load_plugin_filter():
|
||||
filters = defaultdict(frozenset)
|
||||
|
||||
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
|
||||
|
||||
if os.path.exists(filter_cfg):
|
||||
with open(filter_cfg, 'rb') as f:
|
||||
try:
|
||||
filter_data = from_yaml(f.read())
|
||||
except Exception as e:
|
||||
display.warning(u'The plugin filter file, {0} was not parsable.'
|
||||
u' Skipping: {1}'.format(filter_cfg, to_text(e)))
|
||||
return filters
|
||||
|
||||
try:
|
||||
version = filter_data['filter_version']
|
||||
except KeyError:
|
||||
display.warning(u'The plugin filter file, {0} was invalid.'
|
||||
u' Skipping.'.format(filter_cfg))
|
||||
return filters
|
||||
|
||||
# Try to convert for people specifying version as a float instead of string
|
||||
version = to_text(version)
|
||||
version = version.strip()
|
||||
|
||||
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'])
|
||||
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.')
|
||||
else:
|
||||
if user_set:
|
||||
display.warning(u'The plugin filter file, {0} does not exist.'
|
||||
u' Skipping.'.format(filter_cfg))
|
||||
|
||||
# Specialcase the stat module as Ansible can run very few things if stat is blacklisted.
|
||||
if 'stat' in filters['ansible.modules']:
|
||||
raise AnsibleError('The stat module was specified in the module blacklist file, {0}, but'
|
||||
' Ansible will not function without the stat module. Please remove stat'
|
||||
' from the blacklist.'.format(filter_cfg))
|
||||
return filters
|
||||
|
||||
|
||||
# TODO: All of the following is initialization code It should be moved inside of an initialization
|
||||
# function which is called at some point early in the ansible and ansible-playbook CLI startup.
|
||||
|
||||
_PLUGIN_FILTERS = _load_plugin_filter()
|
||||
|
||||
# doc fragments first
|
||||
fragment_loader = PluginLoader(
|
||||
'ModuleDocFragment',
|
||||
|
@ -470,6 +535,7 @@ fragment_loader = PluginLoader(
|
|||
'',
|
||||
)
|
||||
|
||||
|
||||
action_loader = PluginLoader(
|
||||
'ActionModule',
|
||||
'ansible.plugins.action',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue