Debian Codegraph @

lib/ansible/plugins/lookup/nested.py

raw
1 # (c) 2012, Michael DeHaan <[email protected]>
2 # (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4 from __future__ import annotations
5
6 DOCUMENTATION = """
7 name: nested
8 version_added: "1.1"
9 short_description: composes a list with nested elements of other lists
10 description:
11 - Takes the input lists and returns a list with elements that are lists composed of the elements of the input lists
12 positional: _raw
13 options:
14 _raw:
15 description:
16 - a set of lists
17 required: True
18 """
19
20 EXAMPLES = """
21 - name: give users access to multiple databases
22 community.mysql.mysql_user:
23 name: "{{ item[0] }}"
24 priv: "{{ item[1] }}.*:ALL"
25 append_privs: yes
26 password: "foo"
27 with_nested:
28 - [ 'alice', 'bob' ]
29 - [ 'clientdb', 'employeedb', 'providerdb' ]
30 # As with the case of 'with_items' above, you can use previously defined variables.:
31
32 - name: here, 'users' contains the above list of employees
33 community.mysql.mysql_user:
34 name: "{{ item[0] }}"
35 priv: "{{ item[1] }}.*:ALL"
36 append_privs: yes
37 password: "foo"
38 with_nested:
39 - "{{ users }}"
40 - [ 'clientdb', 'employeedb', 'providerdb' ]
41 """
42
43 RETURN = """
44 _list:
45 description:
46 - A list composed of lists paring the elements of the input lists
47 type: list
48 """
49
50
51 from ansible.errors import AnsibleError
52 from ansible.plugins.lookup import LookupBase
53
54
55 class LookupModule(LookupBase):
56 def run(self, terms, variables=None, **kwargs):
57 my_list = terms[:]
58 my_list.reverse()
59
60 if len(my_list) == 0:
61 raise AnsibleError("with_nested requires at least one element in the nested list")
62 result = my_list.pop()
63 while len(my_list) > 0:
64 result2 = self._combine(result, my_list.pop())
65 result = result2
66 new_result = []
67 for x in result:
68 new_result.append(self._flatten(x))
69 return new_result