summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/loops/tasks/main.yml
blob: 03c7c440d8469e61d5e004256c1761a4dc79e62d (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#
# loop_control/pause
#

- name: Measure time before
  shell: date +%s
  register: before

- debug:
    var: i
  with_sequence: count=3
  loop_control:
    loop_var: i
    pause: 2

- name: Measure time after
  shell: date +%s
  register: after

# since there is 3 rounds, and 2 seconds between, it should last 4 seconds
# we do not test the upper bound, since CI can lag significantly
- assert:
    that:
    - '(after.stdout |int) - (before.stdout|int) >= 4'

- name: test subsecond pause
  block:
    - name: Measure time before loop with .5s pause
      set_fact:
        times: "{{times|default([]) + [ now(fmt='%s.%f') ]}}"
      with_sequence: count=3
      loop_control:
        pause: 0.6

    - name: Debug times var
      debug:
        var: times

    - vars:
        tdiff: '{{ times[2]|float - times[0]|float }}'
      block:
        - name: Debug tdiff used in next task
          debug:
            msg: 'tdiff={{ tdiff }}'

        - name: ensure lag, since there is 3 rounds, and 0.5 seconds between, it should last 1.2 seconds, but allowing leeway due to CI lag
          assert:
            that:
                - tdiff|float >= 1.2
                - tdiff|int < 3

#
# Tests of loop syntax with args
#

- name: Test that with_list works with a list
  ping:
    data: '{{ item }}'
  with_list:
    - 'Hello World'
    - 'Olá Mundo'
  register: results

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results["results"][0]["ping"] == "Hello World"'
      - 'results["results"][1]["ping"] == "Olá Mundo"'

- name: Test that with_list works with a list inside a variable
  ping:
    data: '{{ item }}'
  with_list: '{{ phrases }}'
  register: results2

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results2["results"][0]["ping"] == "Hello World"'
      - 'results2["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a manual list
  ping:
    data: '{{ item }}'
  loop:
    - 'Hello World'
    - 'Olá Mundo'
  register: results3

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results3["results"][0]["ping"] == "Hello World"'
      - 'results3["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list in a variable
  ping:
    data: '{{ item }}'
  loop: '{{ phrases }}'
  register: results4

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results4["results"][0]["ping"] == "Hello World"'
      - 'results4["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list via the list lookup
  ping:
    data: '{{ item }}'
  loop: '{{ lookup("list", "Hello World", "Olá Mundo", wantlist=True) }}'
  register: results5

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results5["results"][0]["ping"] == "Hello World"'
      - 'results5["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list in a variable via the list lookup
  ping:
    data: '{{ item }}'
  loop: '{{ lookup("list", wantlist=True, *phrases) }}'
  register: results6

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results6["results"][0]["ping"] == "Hello World"'
      - 'results6["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list via the query lookup
  ping:
    data: '{{ item }}'
  loop: '{{ query("list", "Hello World", "Olá Mundo") }}'
  register: results7

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results7["results"][0]["ping"] == "Hello World"'
      - 'results7["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list in a variable via the query lookup
  ping:
    data: '{{ item }}'
  loop: '{{ q("list", *phrases) }}'
  register: results8

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results8["results"][0]["ping"] == "Hello World"'
      - 'results8["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list and keyword args
  ping:
    data: '{{ item }}'
  loop: '{{ q("file", "data1.txt", "data2.txt", lstrip=True) }}'
  register: results9

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results9["results"][0]["ping"] == "Hello World"'
      - 'results9["results"][1]["ping"] == "Olá Mundo"'

- name: Test that loop works with a list in variable and keyword args
  ping:
    data: '{{ item }}'
  loop: '{{ q("file", lstrip=True, *filenames) }}'
  register: results10

- name: Assert that we ran the module twice with the correct strings
  assert:
    that:
      - 'results10["results"][0]["ping"] == "Hello World"'
      - 'results10["results"][1]["ping"] == "Olá Mundo"'

#
# loop_control/index_var
#

- name: check that the index var is created and increments as expected
  assert:
    that: my_idx == item|int
  with_sequence: start=0 count=3
  loop_control:
    index_var: my_idx

- name: check that value of index var matches position of current item in source list
  assert:
    that: 'test_var.index(item) == my_idx'
  vars:
    test_var: ['a', 'b', 'c']
  with_items: "{{ test_var }}"
  loop_control:
    index_var: my_idx

- name: check index var with included tasks file
  include_tasks: index_var_tasks.yml
  with_sequence: start=0 count=3
  loop_control:
    index_var: my_idx


# The following test cases are to ensure that we don't have a regression on
# GitHub Issue https://github.com/ansible/ansible/issues/35481
#
# This should execute and not cause a RuntimeError
- debug:
    msg: "with_dict passed a list: {{item}}"
  with_dict: "{{ a_list }}"
  register: with_dict_passed_a_list
  ignore_errors: True
  vars:
    a_list:
      - 1
      - 2
- assert:
    that:
      - with_dict_passed_a_list is failed
      - '"with_dict expects a dict" in with_dict_passed_a_list.msg'
- debug:
    msg: "with_list passed a dict: {{item}}"
  with_list: "{{ a_dict }}"
  register: with_list_passed_a_dict
  ignore_errors: True
  vars:
    a_dict:
      key: value
- assert:
    that:
      - with_list_passed_a_dict is failed
      - '"with_list expects a list" in with_list_passed_a_dict.msg'

- debug:
    var: "item"
  loop:
    - "{{ ansible_search_path }}"
  register: loop_search_path

- assert:
    that:
      - ansible_search_path == loop_search_path.results.0.item

# https://github.com/ansible/ansible/issues/45189
- name: with_X conditional delegate_to shortcircuit on templating error
  debug:
    msg: "loop"
  when: false
  delegate_to: localhost
  with_list: "{{ fake_var }}"
  register: result
  failed_when: result is not skipped

- name: loop conditional delegate_to shortcircuit on templating error
  debug:
      msg: "loop"
  when: false
  delegate_to: localhost
  loop: "{{ fake_var }}"
  register: result
  failed_when: result is not skipped

- name: Loop on literal empty list
  debug:
  loop: []
  register: literal_empty_list
  failed_when: literal_empty_list is not skipped

# https://github.com/ansible/ansible/issues/47372
- name: Loop unsafe list
  debug:
    var: item
  with_items: "{{ things|list|unique }}"
  vars:
    things:
      - !unsafe foo
      - !unsafe bar

- name: extended loop info
  assert:
    that:
      - ansible_loop.nextitem == 'orange'
      - ansible_loop.index == 1
      - ansible_loop.index0 == 0
      - ansible_loop.first
      - not ansible_loop.last
      - ansible_loop.previtem is undefined
      - ansible_loop.allitems == ['apple', 'orange', 'banana']
      - ansible_loop.revindex == 3
      - ansible_loop.revindex0 == 2
      - ansible_loop.length == 3
  loop:
    - apple
    - orange
    - banana
  loop_control:
    extended: true
  when: item == 'apple'

- name: extended loop info 2
  assert:
    that:
      - ansible_loop.nextitem == 'banana'
      - ansible_loop.index == 2
      - ansible_loop.index0 == 1
      - not ansible_loop.first
      - not ansible_loop.last
      - ansible_loop.previtem == 'apple'
      - ansible_loop.allitems == ['apple', 'orange', 'banana']
      - ansible_loop.revindex == 2
      - ansible_loop.revindex0 == 1
      - ansible_loop.length == 3
  loop:
    - apple
    - orange
    - banana
  loop_control:
    extended: true
  when: item == 'orange'

- name: extended loop info 3
  assert:
    that:
      - ansible_loop.nextitem is undefined
      - ansible_loop.index == 3
      - ansible_loop.index0 == 2
      - not ansible_loop.first
      - ansible_loop.last
      - ansible_loop.previtem == 'orange'
      - ansible_loop.allitems == ['apple', 'orange', 'banana']
      - ansible_loop.revindex == 1
      - ansible_loop.revindex0 == 0
      - ansible_loop.length == 3
  loop:
    - apple
    - orange
    - banana
  loop_control:
    extended: true
  when: item == 'banana'

- name: Validate the loop_var name
  assert:
    that:
      - ansible_loop_var == 'alvin'
  loop:
    - 1
  loop_control:
    loop_var: alvin

# https://github.com/ansible/ansible/issues/58820
- name: Test using templated loop_var inside include_tasks
  include_tasks: templated_loop_var_tasks.yml
  loop:
    - value
  loop_control:
    loop_var: "{{ loop_var_name }}"
  vars:
    loop_var_name: templated_loop_var_name

# https://github.com/ansible/ansible/issues/59414
- name: Test preserving original connection related vars
  debug:
    var: ansible_remote_tmp
  vars:
    ansible_remote_tmp: /tmp/test1
  with_items:
  - 1
  - 2
  register: loop_out

- assert:
    that:
      - loop_out['results'][1]['ansible_remote_tmp'] == '/tmp/test1'

# https://github.com/ansible/ansible/issues/64169
- include_vars: 64169.yml

- set_fact: "{{ item.key }}={{ hostvars[inventory_hostname][item.value] }}"
  with_dict:
    foo: __foo

- debug:
    var: foo

- assert:
    that:
      - foo[0] != 'foo1.0'
      - foo[0] == unsafe_value
  vars:
    unsafe_value: !unsafe 'foo{{ version_64169 }}'

- set_fact: "{{ item.key }}={{ hostvars[inventory_hostname][item.value] }}"
  loop: "{{ dicty_dict|dict2items }}"
  vars:
    dicty_dict:
      foo: __foo

- debug:
    var: foo

- assert:
    that:
      - foo[0] == 'foo1.0'