Skip to content

Commit

Permalink
fix handling legacy/builtin vars plugins that need to be enabled - do…
Browse files Browse the repository at this point in the history
…n't cache in all()
  • Loading branch information
s-hertel committed Nov 23, 2023
1 parent 383e0ce commit a97f73b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
12 changes: 10 additions & 2 deletions lib/ansible/plugins/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,8 +1097,16 @@ def all(self, *args, **kwargs):
self._update_object(obj, basename, path, resolved=fqcn)

if self._plugin_instance_cache is not None:
# Use get_with_context to cache the plugin the first time we see it.
self.get_with_context(fqcn)[0]
needs_enabled = False
if hasattr(obj, 'REQUIRES_ENABLED'):
needs_enabled = obj.REQUIRES_ENABLED
elif hasattr(obj, 'REQUIRES_WHITELIST'):
needs_enabled = obj.REQUIRES_WHITELIST
display.deprecated("The VarsModule class variable 'REQUIRES_WHITELIST' is deprecated. "
"Use 'REQUIRES_ENABLED' instead.", version=2.18)
if not needs_enabled:
# Use get_with_context to cache the plugin the first time we see it.
self.get_with_context(fqcn)[0]

yield obj

Expand Down
31 changes: 10 additions & 21 deletions lib/ansible/vars/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,11 @@

def _prime_vars_loader():
# find 3rd party legacy vars plugins once, and look them up by name subsequently
for auto_run_plugin in vars_loader.all(class_only=True):
needs_enabled = False
if hasattr(auto_run_plugin, 'REQUIRES_ENABLED'):
needs_enabled = auto_run_plugin.REQUIRES_ENABLED
elif hasattr(auto_run_plugin, 'REQUIRES_WHITELIST'):
needs_enabled = auto_run_plugin.REQUIRES_WHITELIST
display.deprecated("The VarsModule class variable 'REQUIRES_WHITELIST' is deprecated. "
"Use 'REQUIRES_ENABLED' instead.", version=2.18)
if needs_enabled:
del vars_loader._plugin_instance_cache[auto_run_plugin.ansible_name]

list(vars_loader.all(class_only=True))
for plugin_name in C.VARIABLE_PLUGINS_ENABLED:
if not plugin_name:
continue
if (plugin := vars_loader.get(plugin_name)) is not None:
collection = '.' in plugin.ansible_name and not plugin.ansible_name.startswith('ansible.builtin.')
# Warn if a collection plugin has REQUIRES_ENABLED because it has no effect.
if collection and (hasattr(plugin, 'REQUIRES_ENABLED') or hasattr(plugin, 'REQUIRES_WHITELIST')):
display.warning(
"Vars plugins in collections must be enabled to be loaded, REQUIRES_ENABLED is not supported. "
"This should be removed from the plugin %s." % plugin.ansible_name
)
vars_loader.get(plugin_name)


def get_plugin_vars(loader, plugin, path, entities):
Expand Down Expand Up @@ -96,9 +79,7 @@ def _plugin_should_run(plugin, stage):


def get_vars_from_path(loader, path, entities, stage):

data = {}

# FIXME: _prime_vars_loader shouldn't run repeatedly if there are no plugins
if not vars_loader._plugin_instance_cache:
_prime_vars_loader()
Expand All @@ -107,6 +88,14 @@ def get_vars_from_path(loader, path, entities, stage):
if (plugin := vars_loader.get(plugin_name)) is None:
continue

collection = '.' in plugin.ansible_name and not plugin.ansible_name.startswith('ansible.builtin.')
# Warn if a collection plugin has REQUIRES_ENABLED because it has no effect.
if collection and (hasattr(plugin, 'REQUIRES_ENABLED') or hasattr(plugin, 'REQUIRES_WHITELIST')):
display.warning(
"Vars plugins in collections must be enabled to be loaded, REQUIRES_ENABLED is not supported. "
"This should be removed from the plugin %s." % plugin.ansible_name
)

if not _plugin_should_run(plugin, stage):
continue

Expand Down

0 comments on commit a97f73b

Please sign in to comment.