Debian Codegraph @

lib/ansible/plugins/action/set_stats.py

raw
1 # Copyright 2016 Ansible (RedHat, 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
18 from __future__ import annotations
19
20 from ansible.module_utils.parsing.convert_bool import boolean
21 from ansible.plugins.action import ActionBase
22 from ansible.utils.vars import validate_variable_name
23
24
25 class ActionModule(ActionBase):
26
27 TRANSFERS_FILES = False
28 _VALID_ARGS = frozenset(('aggregate', 'data', 'per_host'))
29 _requires_connection = False
30
31 # TODO: document this in non-empty set_stats.py module
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 stats = {'data': {}, 'per_host': False, 'aggregate': True}
40
41 if self._task.args:
42 data = self._task.args.get('data', {})
43
44 if not isinstance(data, dict):
45 data = self._templar.template(data)
46
47 if not isinstance(data, dict):
48 result['failed'] = True
49 result['msg'] = "The 'data' option needs to be a dictionary/hash"
50 return result
51
52 # set boolean options, defaults are set above in stats init
53 for opt in ['per_host', 'aggregate']:
54 val = self._task.args.get(opt, None)
55 if val is not None:
56 if not isinstance(val, bool):
57 stats[opt] = boolean(self._templar.template(val), strict=False)
58 else:
59 stats[opt] = val
60
61 for (k, v) in data.items():
62 k = self._templar.template(k)
63
64 validate_variable_name(k)
65
66 stats['data'][k] = self._templar.template(v)
67
68 result['changed'] = False
69 result['ansible_stats'] = stats
70
71 return result