Debian Codegraph @

lib/ansible/inventory/helpers.py

raw
1 # (c) 2017, Ansible by 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 #############################################
19 from __future__ import annotations
20
21 from ansible._internal._datatag._tags import TrustedAsTemplate
22 from ansible.utils.vars import combine_vars
23
24
25 def sort_groups(groups):
26 return sorted(groups, key=lambda g: (g.depth, g.priority, g.name))
27
28
29 def get_group_vars(groups):
30 """
31 Combine all the group vars from a list of inventory groups.
32
33 :param groups: list of ansible.inventory.group.Group objects
34 :rtype: dict
35 """
36 results = {}
37 for group in sort_groups(groups):
38 results = combine_vars(results, group.get_vars())
39
40 return results
41
42
43 def remove_trust(value: str) -> str:
44 """
45 Remove trust from strings which should not be trusted.
46 This exists to centralize the untagging call which facilitate patching it out in unit tests.
47 """
48 return TrustedAsTemplate.untag(value)