Debian Codegraph @

lib/ansible/plugins/action/service.py

raw
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.plugins.action import ActionBase
22
23
24 class ActionModule(ActionBase):
25
26 TRANSFERS_FILES = False
27
28 UNUSED_PARAMS = {
29 'systemd': ['pattern', 'runlevel', 'sleep', 'arguments', 'args'],
30 }
31
32 # HACK: list of unqualified service manager names that are/were built-in, we'll prefix these with `ansible.legacy` to
33 # avoid collisions with collections search
34 BUILTIN_SVC_MGR_MODULES = set(['openwrt_init', 'service', 'systemd', 'sysvinit'])
35
36 def run(self, tmp=None, task_vars=None):
37 """ handler for package operations """
38
39 self._supports_check_mode = True
40 self._supports_async = True
41
42 super(ActionModule, self).run(tmp, task_vars)
43 del tmp # tmp no longer has any effect
44
45 module = self._task.args.get('use', 'auto').lower()
46
47 if module == 'auto':
48 try:
49 # if we delegate, we should use delegated host's facts
50 expr = "hostvars[delegate_to].ansible_facts.service_mgr" if self._task.delegate_to else "ansible_facts.service_mgr"
51 module = self._templar.resolve_variable_expression(expr, local_variables=dict(delegate_to=self._task.delegate_to))
52 except Exception:
53 pass # could not get it from template!
54
55 try:
56 if module == 'auto':
57 facts = self._execute_module(
58 module_name='ansible.legacy.setup',
59 module_args=dict(gather_subset='!all', filter='ansible_service_mgr'), task_vars=task_vars)
60 self._display.debug("Facts %s" % facts)
61 module = facts.get('ansible_facts', {}).get('ansible_service_mgr', 'auto')
62
63 if not module or module == 'auto' or not self._shared_loader_obj.module_loader.has_plugin(module):
64 module = 'ansible.legacy.service'
65
66 if module != 'auto':
67 # run the 'service' module
68 new_module_args = self._task.args.copy()
69 if 'use' in new_module_args:
70 del new_module_args['use']
71
72 if module in self.UNUSED_PARAMS:
73 for unused in self.UNUSED_PARAMS[module]:
74 if unused in new_module_args:
75 del new_module_args[unused]
76 self._display.warning('Ignoring "%s" as it is not used in "%s"' % (unused, module))
77
78 # get defaults for specific module
79 context = self._shared_loader_obj.module_loader.find_plugin_with_context(module, collection_list=self._task.collections)
80 new_module_args = _apply_action_arg_defaults(context.resolved_fqcn, self._task, new_module_args, self._templar)
81
82 # collection prefix known internal modules to avoid collisions from collections search, while still allowing library/ overrides
83 if module in self.BUILTIN_SVC_MGR_MODULES:
84 module = 'ansible.legacy.' + module
85
86 self._display.vvvv("Running %s" % module)
87 return self._execute_module(module_name=module, module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val)
88 else:
89 raise AnsibleActionFail('Could not detect which service manager to use. Try gathering facts or setting the "use" option.')
90
91 finally:
92 if not self._task.async_val:
93 self._remove_tmp_path(self._connection._shell.tmpdir)