-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubelements-lookup.yml
97 lines (91 loc) · 2.78 KB
/
subelements-lookup.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Essentially, `with_subelements` calculates the Cartesian product of the two
# input lists. The special thing is: the second list is contained as a
# sub-element in the elements of the first list.
# Modified based on the following example:
# https://docs.ansible.com/ansible/latest/collections/ansible/builtin/subelements_lookup.html
#
# See the following link for how to replace `with_subelements` with `loop`:
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-subelements
- name: Demo subelements lookup.
hosts: all
gather_facts: no
vars:
users:
- name: alice
authorized_keys:
- alice_first_authorized_key
- alice_second_authorized_key
mysql:
password: mysql-password
hosts:
- "%"
- "127.0.0.1"
- "::1"
- "localhost"
privs:
- "*.*:SELECT"
- "DB1.*:ALL"
groups:
- wheel
- name: bob
authorized_keys:
- bob_first_authorized_key
mysql:
password: other-mysql-password
hosts:
- "db1"
privs:
- "*.*:SELECT"
- "DB2.*:ALL"
tasks:
- name: Set authorized ssh key, extracting just that data from 'users'
debug:
msg: "user = {{ item.0.name }}; key = {{ item.1 }}"
with_subelements:
- "{{ users }}"
- authorized_keys
- name: Setup MySQL users, given the mysql hosts and privs subkey lists
vars:
mysql_user:
name: "{{ item.0.name }}"
password: "{{ item.0.mysql.password }}"
host: "{{ item.1 }}"
priv: "{{ item.0.mysql.privs | join('/') }}"
debug:
var: mysql_user
with_subelements:
- "{{ users }}"
- mysql.hosts
- name: list groups for users that have them, don't error if groups key is missing
debug:
var: item
loop: "{{ q('subelements', users, 'groups', {'skip_missing': True}) }}"
# See https://stackoverflow.com/a/41908853/630364
- name: Demo subelements lookup (with simple data).
hosts: all
gather_facts: no
vars:
families:
- surname: Smith
country: US
children:
- name: Mike
age: 4
- name: Kate
age: 7
- surname: Sanders
country: UK
children:
- name: Pete
age: 12
- name: Sara
age: 17
tasks:
- name: List children
debug:
msg: "Family={{ item.0.surname }} Child={{ item.1.name }} Age={{ item.1.age }}"
with_subelements:
- "{{ families }}"
- children
# Using `country` will result in the error "the key country should point to a list, got 'US'".
# - country