Debian Codegraph @

lib/ansible/plugins/action/dnf.py

raw
1 # Copyright: (c) 2023, Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3 from __future__ import annotations
4
5 from ansible.errors import AnsibleActionFail
6 from ansible.plugins.action import ActionBase
7 from ansible.utils.display import Display
8
9 display = Display()
10
11 VALID_BACKENDS = frozenset(("yum", "yum4", "dnf", "dnf4", "dnf5"))
12
13
14 class ActionModule(ActionBase):
15
16 TRANSFERS_FILES = False
17
18 def run(self, tmp=None, task_vars=None):
19 self._supports_check_mode = True
20 self._supports_async = True
21
22 result = super(ActionModule, self).run(tmp, task_vars)
23 del tmp # tmp no longer has any effect
24
25 # Carry-over concept from the package action plugin
26 if 'use' in self._task.args and 'use_backend' in self._task.args:
27 raise AnsibleActionFail("parameters are mutually exclusive: ('use', 'use_backend')")
28
29 module = self._task.args.get('use', self._task.args.get('use_backend', 'auto'))
30
31 if module in {'yum', 'auto'}:
32 try:
33 # if we delegate, we should use delegated host's facts
34 expr = "hostvars[delegate_to].ansible_facts.pkg_mgr" if self._task.delegate_to else "ansible_facts.pkg_mgr"
35 module = self._templar.resolve_variable_expression(expr, local_variables=dict(delegate_to=self._task.delegate_to))
36 except Exception:
37 pass # could not get it from template!
38
39 if module not in VALID_BACKENDS:
40 facts = self._execute_module(
41 module_name="ansible.legacy.setup", module_args=dict(filter="ansible_pkg_mgr", gather_subset="!all"),
42 task_vars=task_vars)
43
44 if facts.get("failed", False):
45 raise AnsibleActionFail(
46 f"Failed to fetch ansible_pkg_mgr to determine the dnf action backend: {facts.get('msg')}",
47 result=facts,
48 )
49
50 display.debug("Facts %s" % facts)
51 module = facts.get("ansible_facts", {}).get("ansible_pkg_mgr", "auto")
52 if (not self._task.delegate_to or self._task.delegate_facts) and module != 'auto':
53 result['ansible_facts'] = {'pkg_mgr': module}
54
55 if module not in VALID_BACKENDS:
56 result.update(
57 {
58 'failed': True,
59 'msg': ("Could not detect which major revision of dnf is in use, which is required to determine module backend.",
60 "You should manually specify use_backend to tell the module whether to use the dnf4 or dnf5 backend})"),
61 }
62 )
63
64 else:
65 if module in {"yum4", "dnf4"}:
66 module = "dnf"
67
68 # eliminate collisions with collections search while still allowing local override
69 module = 'ansible.legacy.' + module
70
71 if not self._shared_loader_obj.module_loader.has_plugin(module):
72 result.update({'failed': True, 'msg': "Could not find a dnf module backend for %s." % module})
73 else:
74 new_module_args = self._task.args.copy()
75 if 'use_backend' in new_module_args:
76 del new_module_args['use_backend']
77 if 'use' in new_module_args:
78 del new_module_args['use']
79
80 display.vvvv("Running %s as the backend for the dnf action plugin" % module)
81 result.update(self._execute_module(
82 module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val))
83
84 return result