Debian Codegraph @

lib/ansible/plugins/action/group_by.py

raw
1 # Copyright 2012, Jeroen Hoekx <[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 from __future__ import annotations
18
19 from ansible.plugins.action import ActionBase
20
21
22 class ActionModule(ActionBase):
23 """ Create inventory groups based on variables """
24
25 # We need to be able to modify the inventory
26 TRANSFERS_FILES = False
27 _VALID_ARGS = frozenset(('key', 'parents'))
28 _requires_connection = False
29
30 def run(self, tmp=None, task_vars=None):
31 if task_vars is None:
32 task_vars = dict()
33
34 result = super(ActionModule, self).run(tmp, task_vars)
35 del tmp # tmp no longer has any effect
36
37 if 'key' not in self._task.args:
38 result['failed'] = True
39 result['msg'] = "the 'key' param is required when using group_by"
40 return result
41
42 group_name = self._task.args.get('key')
43
44 parent_groups = self._task.args.get('parents', ['all'])
45 if isinstance(parent_groups, str):
46 parent_groups = [parent_groups]
47
48 result['add_group'] = group_name.replace(' ', '-')
49 result['parent_groups'] = [name.replace(' ', '-') for name in parent_groups]
50 result['changed'] = self.add_group(group_name, parent_group_names=result['parent_groups'])
51
52 return result