| 1 |
# (c) 2015, Ansible Inc, |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
from __future__ import annotations |
| 18 |
|
| 19 |
from ansible.errors import AnsibleActionFail |
| 20 |
from ansible.executor.module_common import _apply_action_arg_defaults |
| 21 |
from ansible.module_utils.facts.system.pkg_mgr import PKG_MGRS |
| 22 |
from ansible.plugins.action import ActionBase |
| 23 |
from ansible.utils.display import Display |
| 24 |
from ansible.utils.vars import combine_vars |
| 25 |
|
| 26 |
display = Display() |
| 27 |
|
| 28 |
|
| 29 |
class ActionModule(ActionBase): |
| 30 |
|
| 31 |
TRANSFERS_FILES = False |
| 32 |
|
| 33 |
BUILTIN_PKG_MGR_MODULES = {manager['name'] for manager in PKG_MGRS} |
| 34 |
|
| 35 |
def run(self, tmp=None, task_vars=None): |
| 36 |
""" handler for package operations """ |
| 37 |
|
| 38 |
self._supports_check_mode = True |
| 39 |
self._supports_async = True |
| 40 |
|
| 41 |
super(ActionModule, self).run(tmp, task_vars) |
| 42 |
|
| 43 |
module = self._task.args.get('use', 'auto') |
| 44 |
|
| 45 |
try: |
| 46 |
if module == 'auto': |
| 47 |
|
| 48 |
if self._task.delegate_to: |
| 49 |
hosts_vars = task_vars['hostvars'][self._task.delegate_to] |
| 50 |
tvars = combine_vars(self._task.vars, task_vars.get('delegated_vars', {})) |
| 51 |
else: |
| 52 |
hosts_vars = task_vars |
| 53 |
tvars = task_vars |
| 54 |
|
| 55 |
# use config |
| 56 |
module = tvars.get('ansible_package_use', None) |
| 57 |
|
| 58 |
if not module: |
| 59 |
# no use, no config, get from facts |
| 60 |
if hosts_vars.get('ansible_facts', {}).get('pkg_mgr', False): |
| 61 |
facts = hosts_vars |
| 62 |
pmgr = 'pkg_mgr' |
| 63 |
else: |
| 64 |
# we had no facts, so generate them |
| 65 |
# very expensive step, we actually run fact gathering because we don't have facts for this host. |
| 66 |
facts = self._execute_module( |
| 67 |
module_name='ansible.legacy.setup', |
| 68 |
module_args=dict(filter='ansible_pkg_mgr', gather_subset='!all'), |
| 69 |
task_vars=task_vars, |
| 70 |
) |
| 71 |
if facts.get("failed", False): |
| 72 |
raise AnsibleActionFail( |
| 73 |
f"Failed to fetch ansible_pkg_mgr to determine the package action backend: {facts.get('msg')}", |
| 74 |
result=facts, |
| 75 |
) |
| 76 |
pmgr = 'ansible_pkg_mgr' |
| 77 |
|
| 78 |
try: |
| 79 |
# actually get from facts |
| 80 |
module = facts['ansible_facts'][pmgr] |
| 81 |
except KeyError: |
| 82 |
raise AnsibleActionFail('Could not detect a package manager. Try using the "use" option.') |
| 83 |
|
| 84 |
if module and module != 'auto': |
| 85 |
if not self._shared_loader_obj.module_loader.has_plugin(module): |
| 86 |
raise AnsibleActionFail('Could not find a matching action for the "%s" package manager.' % module) |
| 87 |
else: |
| 88 |
# run the 'package' module |
| 89 |
new_module_args = self._task.args.copy() |
| 90 |
if 'use' in new_module_args: |
| 91 |
del new_module_args['use'] |
| 92 |
|
| 93 |
# get defaults for specific module |
| 94 |
context = self._shared_loader_obj.module_loader.find_plugin_with_context(module, collection_list=self._task.collections) |
| 95 |
new_module_args = _apply_action_arg_defaults(context.resolved_fqcn, self._task, new_module_args, self._templar) |
| 96 |
|
| 97 |
if module in self.BUILTIN_PKG_MGR_MODULES: |
| 98 |
# prefix with ansible.legacy to eliminate external collisions while still allowing library/ override |
| 99 |
module = 'ansible.legacy.' + module |
| 100 |
|
| 101 |
display.vvvv("Running %s" % module) |
| 102 |
return self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val) |
| 103 |
else: |
| 104 |
raise AnsibleActionFail('Could not detect which package manager to use. Try gathering facts or setting the "use" option.') |
| 105 |
finally: |
| 106 |
pass # avoid de-dent all on refactor |