Debian Codegraph @

test/integration/targets/lookup_first_found/tasks/main.yml

raw
1 - name: an invalid term data type results in an error
2 debug:
3 with_first_found:
4 - 1
5 ignore_errors: yes
6 register: result
7
8 - name: verify the invalid term failure
9 assert:
10 that:
11 - result is failed
12 - result.msg is contains "not 'int'"
13
14 - name: an undefined kwarg results in an error
15 debug:
16 msg: "{{ lookup('first_found', files=resolve_to_undefined) }}"
17 vars:
18 resolve_to_undefined: "{{ sorry_not_defined }}"
19 ignore_errors: yes
20 register: result
21
22 - name: verify the undefined kwarg is handled properly
23 assert:
24 that:
25 - result is failed
26 - result.msg is search "Error while resolving value for 'msg'. 'sorry_not_defined' is undefined"
27
28 - name: ensure empty terms mappings are properly handled
29 debug:
30 with_first_found:
31 - {}
32 - foo1
33 register: result
34
35 - name: verify the correct file was found
36 assert:
37 that:
38 - result.results | length == 1
39 - result.results[0].item.endswith('/foo1')
40
41 - name: roles search for files in the files subdir
42 set_fact:
43 find_under_files: "{{ lookup('first_found', 'findme.txt') }}"
44
45 - name: verify the content of the found file
46 assert:
47 that:
48 - lookup('file', find_under_files) == 'under files subdir'
49 - find_under_files.endswith('/findme.txt')
50
51 - name: ensure that files subdir is used when searching for files using include_vars
52 include_vars: "{{ lookup('first_found', 'findme.txt') }}"
53
54 - name: verify the content of the found file
55 assert:
56 that:
57 - "hello == 'world'"
58
59 - name: ensure that files subdir is used when searching for files using template
60 template:
61 src: "{{ lookup('first_found', 'findme.txt') }}"
62 dest: /tmp/sample.txt
63 register: template_result
64
65 - name: check the result of template task
66 assert:
67 that:
68 - template_result is succeeded
69 - lookup('file', '/tmp/sample.txt') == 'world'
70
71 - name: ensure that files subdir is used when searching for files using include_tasks
72 include_tasks: "{{ lookup('first_found', 'findme.yml') }}"
73
74 - name: verify the content of the found file
75 assert:
76 that:
77 - "sample_var == 'Hello World'"
78
79 - name: ensure template errors are properly handled when using with_first_found
80 debug:
81 with_first_found:
82 - "{{ 1 / 0 }}"
83 ignore_errors: yes
84 register: result
85
86 - name: check the result of the template error
87 assert:
88 that:
89 - result is failed
90 - result.msg is search "Error rendering template. division by zero"
91
92 - name: undefined values should be ignored
93 debug:
94 msg: "found: {{ item }}"
95 with_first_found:
96 - "{{ missing_var }}foo1"
97 - bar1
98 register: result
99
100 - name: verify the correct file was found
101 assert:
102 that:
103 result.results[0].item.endswith('/bar1')
104
105 - name: undefined values in tuples should be ignored
106 debug:
107 msg: "found: {{ item }}"
108 with_first_found: '{{ ([{"files": missing_var}] | zip([{"files": ["bar1"]}])) }}'
109 register: result
110
111 - name: verify the correct file was found
112 assert:
113 that:
114 result.results[0].item.endswith('/bar1')
115
116 - name: verify lazy undefined handling
117 debug:
118 msg: "found: {{ item }}"
119 with_first_found:
120 - files: "{{ files }}"
121 register: result
122 vars:
123 files:
124 - "{{ missing_var }}foo1"
125 - bar1
126 - name: verify the correct file was found
127 assert:
128 that:
129 result.results[0].item.endswith('/bar1')
130
131 - name: test with_first_found
132 set_fact: "first_found={{ item }}"
133 with_first_found:
134 - "does_not_exist"
135 - "foo1"
136 - "{{ role_path + '/files/bar1' }}" # will only hit this if dwim search is broken
137
138 - name: set expected
139 set_fact: first_expected="{{ role_path + '/files/foo1' }}"
140
141 - name: set unexpected
142 set_fact: first_unexpected="{{ role_path + '/files/bar1' }}"
143
144 - name: verify with_first_found results
145 assert:
146 that:
147 - "first_found == first_expected"
148 - "first_found != first_unexpected"
149
150 - name: test q(first_found) with no files produces empty list
151 set_fact:
152 first_found_var: "{{ q('first_found', params, errors='ignore') }}"
153 vars:
154 params:
155 files: "not_a_file.yaml"
156 skip: True
157
158 - name: verify q(first_found) result
159 assert:
160 that:
161 - "first_found_var == []"
162
163 - name: test lookup(first_found) with no files produces none
164 set_fact:
165 first_found_var: "{{ lookup('first_found', params, errors='ignore') }}"
166 vars:
167 params:
168 files: "not_a_file.yaml"
169
170 - name: verify lookup(first_found) result
171 assert:
172 that:
173 - "first_found_var is none"
174
175 # NOTE: skip: True deprecated e17a2b502d6601be53c60d7ba1c627df419460c9, remove 2.12
176 - name: test first_found with no matches and skip=True does nothing
177 set_fact: "this_not_set={{ item }}"
178 vars:
179 params:
180 files:
181 - not/a/file.yaml
182 - another/non/file.yaml
183 skip: True
184 loop: "{{ q('first_found', params) }}"
185
186 - name: verify skip
187 assert:
188 that:
189 - "this_not_set is not defined"
190
191 - name: test first_found with no matches and errors='ignore' skips in a loop
192 set_fact: "this_not_set={{ item }}"
193 vars:
194 params:
195 files:
196 - not/a/file.yaml
197 - another/non/file.yaml
198 loop: "{{ query('first_found', params, errors='ignore') }}"
199
200 - name: verify errors=ignore
201 assert:
202 that:
203 - "this_not_set is not defined"
204
205 - name: test legacy formats
206 set_fact: hatethisformat={{item}}
207 vars:
208 params:
209 files: not/a/file.yaml;hosts
210 paths: not/a/path:/etc
211 loop: "{{ q('first_found', params) }}"
212
213 - name: verify /etc/hosts was found
214 assert:
215 that:
216 - "hatethisformat == '/etc/hosts'"
217
218 - name: test spaces in names
219 include_vars: "{{ item }}"
220 with_first_found:
221 - files:
222 - "{{ role_path + '/files/vars file spaces.yml' }}"
223
224 - assert:
225 that:
226 - foo is defined
227
228 # TODO: no 'terms' test
229 - name: test first_found lookup with no terms
230 set_fact:
231 no_terms: "{{ query('first_found', files=['missing1', 'hosts', 'missing2'], paths=['/etc'], errors='ignore') }}"
232
233 - assert:
234 that: "no_terms|first == '/etc/hosts'"
235
236 - name: handle templatable dictionary entries
237 block:
238
239 - name: Load variables specific for OS family
240 assert:
241 that:
242 - item is file
243 - item|basename == 'itworks.yml'
244 with_first_found:
245 - files:
246 # DTFIX-FUTURE: these variables are misleading for the test; use "bogus" or something for the ones that are not
247 # expected to exist
248 - "{{ansible_id}}-{{ansible_lsb.major_release}}.yml" # invalid var, should be skipped
249 - "{{ansible_lsb.id}}-{{ansible_lsb.major_release}}.yml" # does not exist, but should try
250 - "{{ansible_distribution}}-{{ansible_distribution_major_version}}.yml" # does not exist, but should try
251 - itworks.yml
252 - ishouldnotbefound.yml # this exist, but should not be found
253 paths:
254 - "{{role_path}}/vars"
255
256 - name: Load variables specific for OS family, but now as list of dicts, same options as above
257 assert:
258 that:
259 - item is file
260 - item|basename == 'itworks.yml'
261 with_first_found:
262 - files:
263 - "{{ansible_id}}-{{ansible_lsb.major_release}}.yml"
264 paths:
265 - "{{role_path}}/vars"
266 - files:
267 - "{{ansible_lsb.id}}-{{ansible_lsb.major_release}}.yml"
268 paths:
269 - "{{role_path}}/vars"
270 - files:
271 - "{{ansible_distribution}}-{{ansible_distribution_major_version}}.yml"
272 paths:
273 - "{{role_path}}/vars"
274 - files:
275 - itworks.yml
276 paths:
277 - "{{role_path}}/vars"
278 - files:
279 - ishouldnotbefound.yml
280 paths:
281 - "{{role_path}}/vars"
282
283 - name: ensure vars subdir is searched for var-named actions
284 # DTFIX5: the following *should* work, but since task.action is not templated by TaskExecutor, it does not
285 # action: '{{ "debug_v" ~ "ar_alias" }}'
286 # args:
287 # var: item
288 debug_var_alias:
289 var: item
290 with_first_found:
291 - findme.txt
292 failed_when: item is not contains "/vars/"
293
294 - name: ensure files subdir is searched for file-named actions
295 debug_file_alias:
296 var: item
297 with_first_found:
298 - findme.txt
299 failed_when: item is not contains "/files/"
300
301 - name: ensure templates subdir is searched for template-named actions
302 debug_template_alias:
303 var: item
304 with_first_found:
305 - findme.txt
306 failed_when: item is not contains "/templates/"
307
308 - name: ensure files subdir is searched for actions not matching `var` or `template`
309 debug:
310 var: item
311 with_first_found:
312 - findme.txt
313 failed_when: item is not contains "/files/"
314
315 - name: Make sure skip works in 'mixed' argument passing
316 assert:
317 that:
318 - q('first_found', ['/nonexistant'], skip=True) == []
319
320 - name: Test relative paths in roles
321 include_role:
322 role: "{{ role_path }}/roles/a"