Debian Codegraph @

lib/ansible/vars/plugins.py

raw
1 # Copyright (c) 2018 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 import os
7
8 from functools import lru_cache
9
10 from ansible import constants as C
11 from ansible.plugins.loader import vars_loader
12 from ansible.utils.display import Display
13 from ansible.utils.vars import combine_vars
14
15 display = Display()
16
17
18 def _prime_vars_loader():
19 # find 3rd party legacy vars plugins once, and look them up by name subsequently
20 list(vars_loader.all(class_only=True))
21 for plugin_name in C.VARIABLE_PLUGINS_ENABLED:
22 if not plugin_name:
23 continue
24 vars_loader.get(plugin_name)
25
26
27 # optimized for stateless plugins; non-stateless plugin instances will fall out quickly
28 @lru_cache(maxsize=10)
29 def _plugin_should_run(plugin, stage):
30 # if a plugin-specific setting has not been provided, use the global setting
31 # older/non shipped plugins that don't support the plugin-specific setting should also use the global setting
32 allowed_stages = None
33
34 try:
35 allowed_stages = plugin.get_option('stage')
36 except (AttributeError, KeyError):
37 pass
38
39 if allowed_stages:
40 return allowed_stages in ('all', stage)
41
42 # plugin didn't declare a preference; consult global config
43 config_stage_override = C.RUN_VARS_PLUGINS
44 if config_stage_override == 'demand' and stage == 'inventory':
45 return False
46 elif config_stage_override == 'start' and stage == 'task':
47 return False
48 return True
49
50
51 def get_vars_from_path(loader, path, entities, stage):
52 data = {}
53 if vars_loader._paths is None:
54 # cache has been reset, reload all()
55 _prime_vars_loader()
56
57 for plugin_name in vars_loader._plugin_instance_cache:
58 if (plugin := vars_loader.get(plugin_name)) is None:
59 continue
60
61 collection = '.' in plugin.ansible_name and not plugin.ansible_name.startswith('ansible.builtin.')
62 # Warn if a collection plugin has REQUIRES_ENABLED because it has no effect.
63 if collection and hasattr(plugin, 'REQUIRES_ENABLED'):
64 display.warning(
65 "Vars plugins in collections must be enabled to be loaded, REQUIRES_ENABLED is not supported. "
66 "This should be removed from the plugin %s." % plugin.ansible_name
67 )
68
69 if not _plugin_should_run(plugin, stage):
70 continue
71
72 if (new_vars := plugin.get_vars(loader, path, entities)) != {}:
73 data = combine_vars(data, new_vars)
74
75 return data
76
77
78 def get_vars_from_inventory_sources(loader, sources, entities, stage):
79
80 data = {}
81 for path in sources:
82
83 if path is None:
84 continue
85 if ',' in path and not os.path.exists(path): # skip host lists
86 continue
87 elif not os.path.isdir(path):
88 # always pass the directory of the inventory source file
89 path = os.path.dirname(path)
90
91 if (new_vars := get_vars_from_path(loader, path, entities, stage)) != {}:
92 data = combine_vars(data, new_vars)
93
94 return data