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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
- name: test with_subelements
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
with_subelements:
- "{{element_data}}"
- the_list
- name: verify with_subelements results
assert:
that:
- "_xf == 'f'"
- "_xd == 'd'"
- "_ye == 'e'"
- "_yf == 'f'"
- name: test with_subelements in subkeys
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
with_subelements:
- "{{element_data}}"
- the.sub.key.list
- name: verify with_subelements in subkeys results
assert:
that:
- "_xq == 'q'"
- "_xr == 'r'"
- "_yi == 'i'"
- "_yo == 'o'"
- name: test with_subelements with missing key or subkey
set_fact: "{{ '_'+ item.0.id + item.1 }}={{ item.1 }}"
with_subelements:
- "{{element_data_missing}}"
- the.sub.key.list
- skip_missing: yes
register: _subelements_missing_subkeys
- debug: var=_subelements_missing_subkeys
- debug: var=_subelements_missing_subkeys.results|length
- name: verify with_subelements in subkeys results
assert:
that:
- _subelements_missing_subkeys is not skipped
- _subelements_missing_subkeys.results|length == 2
- "_xk == 'k'"
- "_xl == 'l'"
# Example from the DOCUMENTATION block
- set_fact:
users:
- name: alice
authorized:
- /tmp/alice/onekey.pub
- /tmp/alice/twokey.pub
mysql:
password: mysql-password
hosts:
- "%"
- "127.0.0.1"
- "::1"
- "localhost"
privs:
- "*.*:SELECT"
- "DB1.*:ALL"
groups:
- wheel
- name: bob
authorized:
- /tmp/bob/id_rsa.pub
mysql:
password: other-mysql-password
hosts:
- "db1"
privs:
- "*.*:SELECT"
- "DB2.*:ALL"
- name: carol
skipped: true
authorized:
- /tmp/carol/id_rsa.pub
mysql:
password: third-mysql-password
hosts:
- "db9"
privs:
- "*.*:SELECT"
- "DB9.*:ALL"
- name: Ensure it errors properly with non-dict
set_fact:
err: "{{ lookup('subelements', 9001, 'groups', wantlist=true) }}"
ignore_errors: true
register: err1
- assert:
that:
- err1 is failed
- "'expects a dictionary' in err1.msg"
- name: Ensure it errors properly when pointing to non-list
set_fact:
err: "{{ lookup('subelements', users, 'mysql.password', wantlist=true) }}"
ignore_errors: true
register: err2
- assert:
that:
- err2 is failed
- "'should point to a list' in err2.msg"
- name: Ensure it properly skips missing keys
set_fact:
err: "{{ lookup('subelements', users, 'mysql.hosts.doesnotexist', wantlist=true) }}"
ignore_errors: true
register: err3
- assert:
that:
- err3 is failed
- "'should point to a dictionary' in err3.msg"
- name: Ensure it properly skips missing keys
set_fact:
err: "{{ lookup('subelements', users, 'mysql.monkey', wantlist=true) }}"
ignore_errors: true
register: err4
- assert:
that:
- err4 is failed
- >-
"could not find 'monkey' key in iterated item" in err4.msg
- assert:
that:
- "'{{ item.0.name }}' != 'carol'"
with_subelements:
- "{{ users }}"
- mysql.privs
- name: Ensure it errors properly when optional arg is nonsensical
set_fact:
err: neverset
with_subelements:
- "{{ users }}"
- mysql.privs
- wolves
ignore_errors: true
register: err5
- assert:
that:
- err5 is failed
- "'the optional third item must be a dict' in err5.msg"
- name: Ensure it errors properly when given way too many args
set_fact:
err: neverset
with_subelements:
- "{{ users }}"
- mysql.privs
- wolves
- foo
- bar
- baz
- bye now
ignore_errors: true
register: err6
- assert:
that:
- err6 is failed
- "'expects a list of two or three' in err6.msg"
- name: Ensure it errors properly when second arg is invalid type
set_fact:
err: neverset
with_subelements:
- "{{ users }}"
- true
ignore_errors: true
register: err7
- assert:
that:
- err7 is failed
- "'second a string' in err7.msg"
- name: Ensure it errors properly when first arg is invalid type
set_fact:
err: neverset
with_subelements:
- true
- "{{ users }}"
ignore_errors: true
register: err8
- assert:
that:
- err8 is failed
- "'first a dict or a list' in err8.msg"
- set_fact:
empty_subelements: "{{ lookup('subelements', {'skipped': true}, 'mysql.hosts', wantlist=true) }}"
- assert:
that:
- empty_subelements == []
- set_fact:
some_dict:
key: "{{ users[0] }}"
another: "{{ users[1] }}"
- name: Ensure it works when we give a dict instead of a list
set_fact: "user_{{ item.0.name }}={{ item.1 }}"
with_subelements:
- "{{ some_dict }}"
- mysql.hosts
- assert:
that:
- "'{{ user_alice }}' == 'localhost'"
- "'{{ user_bob }}' == 'db1'"
|