Debian Codegraph @

lib/ansible/plugins/action/set_fact.py

raw
1 # Copyright 2013 Dag Wieers <[email protected]>
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
18 from __future__ import annotations
19
20 from ansible.errors import AnsibleActionFail
21 from ansible.module_utils.parsing.convert_bool import boolean
22 from ansible.plugins.action import ActionBase
23 from ansible.utils.vars import validate_variable_name
24 from . import VariableLayer
25
26
27 class ActionModule(ActionBase):
28
29 TRANSFERS_FILES = False
30 _requires_connection = False
31
32 def run(self, tmp=None, task_vars=None):
33 if task_vars is None:
34 task_vars = dict()
35
36 result = super(ActionModule, self).run(tmp, task_vars)
37 del tmp # tmp no longer has any effect
38
39 facts = {}
40 cacheable = boolean(self._task.args.pop('cacheable', False))
41
42 if self._task.args:
43 for (k, v) in self._task.args.items():
44 k = self._templar.template(k) # a rare case where key templating is allowed; backward-compatibility for dynamic storage
45
46 validate_variable_name(k)
47
48 facts[k] = v
49 else:
50 raise AnsibleActionFail('No key/value pairs provided, at least one is required for this action to succeed')
51
52 if facts:
53 # just as _facts actions, we don't set changed=true as we are not modifying the actual host
54 if cacheable:
55 self.register_host_variables(facts, VariableLayer.CACHEABLE_FACT)
56 else:
57 self.register_host_variables({}, VariableLayer.CACHEABLE_FACT) # disable legacy behavior of registering 'ansible_facts' from result dict
58
59 self.register_host_variables(facts, VariableLayer.EPHEMERAL_FACT)
60
61 result['ansible_facts'] = facts
62 else:
63 # this should not happen, but JIC we get here
64 raise AnsibleActionFail('Unable to create any variables with provided arguments')
65
66 return result