| 1 |
# |
| 2 |
# loop_control/pause |
| 3 |
# |
| 4 |
|
| 5 |
- name: Measure time before |
| 6 |
shell: date +%s |
| 7 |
register: before |
| 8 |
|
| 9 |
- debug: |
| 10 |
var: i |
| 11 |
with_sequence: count=3 |
| 12 |
loop_control: |
| 13 |
loop_var: i |
| 14 |
pause: 2 |
| 15 |
|
| 16 |
- name: Measure time after |
| 17 |
shell: date +%s |
| 18 |
register: after |
| 19 |
|
| 20 |
# since there is 3 rounds, and 2 seconds between, it should last 4 seconds |
| 21 |
# we do not test the upper bound, since CI can lag significantly |
| 22 |
- assert: |
| 23 |
that: |
| 24 |
- '(after.stdout |int) - (before.stdout|int) >= 4' |
| 25 |
|
| 26 |
- name: test subsecond pause |
| 27 |
block: |
| 28 |
- name: Measure time before loop with .5s pause |
| 29 |
set_fact: |
| 30 |
times: "{{times|default([]) + [ now(fmt='%s.%f') ]}}" |
| 31 |
with_sequence: count=3 |
| 32 |
loop_control: |
| 33 |
pause: 0.6 |
| 34 |
|
| 35 |
- name: Debug times var |
| 36 |
debug: |
| 37 |
var: times |
| 38 |
|
| 39 |
- vars: |
| 40 |
tdiff: '{{ times[2]|float - times[0]|float }}' |
| 41 |
block: |
| 42 |
- name: Debug tdiff used in next task |
| 43 |
debug: |
| 44 |
msg: 'tdiff={{ tdiff }}' |
| 45 |
|
| 46 |
- 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 |
| 47 |
assert: |
| 48 |
that: |
| 49 |
- tdiff|float >= 1.2 |
| 50 |
- tdiff|int < 3 |
| 51 |
|
| 52 |
# |
| 53 |
# Tests of loop syntax with args |
| 54 |
# |
| 55 |
|
| 56 |
- name: Test that with_list works with a list |
| 57 |
ping: |
| 58 |
data: '{{ item }}' |
| 59 |
with_list: |
| 60 |
- 'Hello World' |
| 61 |
- 'Olá Mundo' |
| 62 |
register: results |
| 63 |
|
| 64 |
- name: Assert that we ran the module twice with the correct strings |
| 65 |
assert: |
| 66 |
that: |
| 67 |
- 'results["results"][0]["ping"] == "Hello World"' |
| 68 |
- 'results["results"][1]["ping"] == "Olá Mundo"' |
| 69 |
|
| 70 |
- name: Test that with_list works with a list inside a variable |
| 71 |
ping: |
| 72 |
data: '{{ item }}' |
| 73 |
with_list: '{{ phrases }}' |
| 74 |
register: results2 |
| 75 |
|
| 76 |
- name: Assert that we ran the module twice with the correct strings |
| 77 |
assert: |
| 78 |
that: |
| 79 |
- 'results2["results"][0]["ping"] == "Hello World"' |
| 80 |
- 'results2["results"][1]["ping"] == "Olá Mundo"' |
| 81 |
|
| 82 |
- name: Test that loop works with a manual list |
| 83 |
ping: |
| 84 |
data: '{{ item }}' |
| 85 |
loop: |
| 86 |
- 'Hello World' |
| 87 |
- 'Olá Mundo' |
| 88 |
register: results3 |
| 89 |
|
| 90 |
- name: Assert that we ran the module twice with the correct strings |
| 91 |
assert: |
| 92 |
that: |
| 93 |
- 'results3["results"][0]["ping"] == "Hello World"' |
| 94 |
- 'results3["results"][1]["ping"] == "Olá Mundo"' |
| 95 |
|
| 96 |
- name: Test that loop works with a list in a variable |
| 97 |
ping: |
| 98 |
data: '{{ item }}' |
| 99 |
loop: '{{ phrases }}' |
| 100 |
register: results4 |
| 101 |
|
| 102 |
- name: Assert that we ran the module twice with the correct strings |
| 103 |
assert: |
| 104 |
that: |
| 105 |
- 'results4["results"][0]["ping"] == "Hello World"' |
| 106 |
- 'results4["results"][1]["ping"] == "Olá Mundo"' |
| 107 |
|
| 108 |
- name: Test that loop works with a list via the list lookup |
| 109 |
ping: |
| 110 |
data: '{{ item }}' |
| 111 |
loop: '{{ lookup("list", "Hello World", "Olá Mundo", wantlist=True) }}' |
| 112 |
register: results5 |
| 113 |
|
| 114 |
- name: Assert that we ran the module twice with the correct strings |
| 115 |
assert: |
| 116 |
that: |
| 117 |
- 'results5["results"][0]["ping"] == "Hello World"' |
| 118 |
- 'results5["results"][1]["ping"] == "Olá Mundo"' |
| 119 |
|
| 120 |
- name: Test that loop works with a list in a variable via the list lookup |
| 121 |
ping: |
| 122 |
data: '{{ item }}' |
| 123 |
loop: '{{ lookup("list", wantlist=True, *phrases) }}' |
| 124 |
register: results6 |
| 125 |
|
| 126 |
- name: Assert that we ran the module twice with the correct strings |
| 127 |
assert: |
| 128 |
that: |
| 129 |
- 'results6["results"][0]["ping"] == "Hello World"' |
| 130 |
- 'results6["results"][1]["ping"] == "Olá Mundo"' |
| 131 |
|
| 132 |
- name: Test that loop works with a list via the query lookup |
| 133 |
ping: |
| 134 |
data: '{{ item }}' |
| 135 |
loop: '{{ query("list", "Hello World", "Olá Mundo") }}' |
| 136 |
register: results7 |
| 137 |
|
| 138 |
- name: Assert that we ran the module twice with the correct strings |
| 139 |
assert: |
| 140 |
that: |
| 141 |
- 'results7["results"][0]["ping"] == "Hello World"' |
| 142 |
- 'results7["results"][1]["ping"] == "Olá Mundo"' |
| 143 |
|
| 144 |
- name: Test that loop works with a list in a variable via the query lookup |
| 145 |
ping: |
| 146 |
data: '{{ item }}' |
| 147 |
loop: '{{ q("list", *phrases) }}' |
| 148 |
register: results8 |
| 149 |
|
| 150 |
- name: Assert that we ran the module twice with the correct strings |
| 151 |
assert: |
| 152 |
that: |
| 153 |
- 'results8["results"][0]["ping"] == "Hello World"' |
| 154 |
- 'results8["results"][1]["ping"] == "Olá Mundo"' |
| 155 |
|
| 156 |
- name: Test that loop works with a list and keyword args |
| 157 |
ping: |
| 158 |
data: '{{ item }}' |
| 159 |
loop: '{{ q("file", "data1.txt", "data2.txt", lstrip=True) }}' |
| 160 |
register: results9 |
| 161 |
|
| 162 |
- name: Assert that we ran the module twice with the correct strings |
| 163 |
assert: |
| 164 |
that: |
| 165 |
- 'results9["results"][0]["ping"] == "Hello World"' |
| 166 |
- 'results9["results"][1]["ping"] == "Olá Mundo"' |
| 167 |
|
| 168 |
- name: Test that loop works with a list in variable and keyword args |
| 169 |
ping: |
| 170 |
data: '{{ item }}' |
| 171 |
loop: '{{ q("file", lstrip=True, *filenames) }}' |
| 172 |
register: results10 |
| 173 |
|
| 174 |
- name: Assert that we ran the module twice with the correct strings |
| 175 |
assert: |
| 176 |
that: |
| 177 |
- 'results10["results"][0]["ping"] == "Hello World"' |
| 178 |
- 'results10["results"][1]["ping"] == "Olá Mundo"' |
| 179 |
|
| 180 |
# |
| 181 |
# loop_control/index_var |
| 182 |
# |
| 183 |
|
| 184 |
- name: check that the index var is created and increments as expected |
| 185 |
assert: |
| 186 |
that: my_idx == item|int |
| 187 |
with_sequence: start=0 count=3 |
| 188 |
loop_control: |
| 189 |
index_var: my_idx |
| 190 |
|
| 191 |
- name: check that value of index var matches position of current item in source list |
| 192 |
assert: |
| 193 |
that: 'test_var.index(item) == my_idx' |
| 194 |
vars: |
| 195 |
test_var: ['a', 'b', 'c'] |
| 196 |
with_items: "{{ test_var }}" |
| 197 |
loop_control: |
| 198 |
index_var: my_idx |
| 199 |
|
| 200 |
- name: check index var with included tasks file |
| 201 |
include_tasks: index_var_tasks.yml |
| 202 |
with_sequence: start=0 count=3 |
| 203 |
loop_control: |
| 204 |
index_var: my_idx |
| 205 |
|
| 206 |
|
| 207 |
# The following test cases are to ensure that we don't have a regression on |
| 208 |
# GitHub Issue https://github.com/ansible/ansible/issues/35481 |
| 209 |
# |
| 210 |
# This should execute and not cause a RuntimeError |
| 211 |
- debug: |
| 212 |
msg: "with_dict passed a list: {{item}}" |
| 213 |
with_dict: "{{ a_list }}" |
| 214 |
register: with_dict_passed_a_list |
| 215 |
ignore_errors: True |
| 216 |
vars: |
| 217 |
a_list: |
| 218 |
- 1 |
| 219 |
- 2 |
| 220 |
- assert: |
| 221 |
that: |
| 222 |
- with_dict_passed_a_list is failed |
| 223 |
- '"lookup plugin expects a dictionary" in with_dict_passed_a_list.msg' |
| 224 |
|
| 225 |
- vars: |
| 226 |
a_dict: |
| 227 |
key: value |
| 228 |
block: |
| 229 |
- debug: |
| 230 |
var: item |
| 231 |
with_list: "{{ a_dict }}" |
| 232 |
register: with_list_passed_a_dict |
| 233 |
|
| 234 |
- assert: |
| 235 |
that: with_list_passed_a_dict.results[0].item == a_dict |
| 236 |
|
| 237 |
- debug: |
| 238 |
var: "item" |
| 239 |
loop: |
| 240 |
- "{{ ansible_search_path }}" |
| 241 |
register: loop_search_path |
| 242 |
|
| 243 |
- assert: |
| 244 |
that: |
| 245 |
- ansible_search_path == loop_search_path.results.0.item |
| 246 |
|
| 247 |
# https://github.com/ansible/ansible/issues/45189 |
| 248 |
- name: with_X conditional delegate_to shortcircuit on templating error |
| 249 |
debug: |
| 250 |
msg: "loop" |
| 251 |
when: false |
| 252 |
delegate_to: localhost |
| 253 |
with_list: "{{ fake_var }}" |
| 254 |
register: result |
| 255 |
failed_when: result is not skipped |
| 256 |
|
| 257 |
- name: loop conditional delegate_to shortcircuit on templating error |
| 258 |
debug: |
| 259 |
msg: "loop" |
| 260 |
when: false |
| 261 |
delegate_to: localhost |
| 262 |
loop: "{{ fake_var }}" |
| 263 |
register: result |
| 264 |
failed_when: result is not skipped |
| 265 |
|
| 266 |
- name: Loop on literal empty list |
| 267 |
debug: |
| 268 |
loop: [] |
| 269 |
register: literal_empty_list |
| 270 |
failed_when: literal_empty_list is not skipped |
| 271 |
|
| 272 |
# https://github.com/ansible/ansible/issues/47372 |
| 273 |
- name: Loop unsafe list |
| 274 |
debug: |
| 275 |
var: item |
| 276 |
with_items: "{{ things|list|unique }}" |
| 277 |
vars: |
| 278 |
things: |
| 279 |
- !unsafe foo |
| 280 |
- !unsafe bar |
| 281 |
|
| 282 |
- name: extended loop info |
| 283 |
assert: |
| 284 |
that: |
| 285 |
- ansible_loop.nextitem == 'orange' |
| 286 |
- ansible_loop.index == 1 |
| 287 |
- ansible_loop.index0 == 0 |
| 288 |
- ansible_loop.first |
| 289 |
- not ansible_loop.last |
| 290 |
- ansible_loop.previtem is undefined |
| 291 |
- ansible_loop.allitems == ['apple', 'orange', 'banana'] |
| 292 |
- ansible_loop.revindex == 3 |
| 293 |
- ansible_loop.revindex0 == 2 |
| 294 |
- ansible_loop.length == 3 |
| 295 |
loop: |
| 296 |
- apple |
| 297 |
- orange |
| 298 |
- banana |
| 299 |
loop_control: |
| 300 |
extended: true |
| 301 |
when: item == 'apple' |
| 302 |
|
| 303 |
- name: extended loop info 2 |
| 304 |
assert: |
| 305 |
that: |
| 306 |
- ansible_loop.nextitem == 'banana' |
| 307 |
- ansible_loop.index == 2 |
| 308 |
- ansible_loop.index0 == 1 |
| 309 |
- not ansible_loop.first |
| 310 |
- not ansible_loop.last |
| 311 |
- ansible_loop.previtem == 'apple' |
| 312 |
- ansible_loop.allitems == ['apple', 'orange', 'banana'] |
| 313 |
- ansible_loop.revindex == 2 |
| 314 |
- ansible_loop.revindex0 == 1 |
| 315 |
- ansible_loop.length == 3 |
| 316 |
loop: |
| 317 |
- apple |
| 318 |
- orange |
| 319 |
- banana |
| 320 |
loop_control: |
| 321 |
extended: true |
| 322 |
when: item == 'orange' |
| 323 |
|
| 324 |
- name: extended loop info 3 |
| 325 |
assert: |
| 326 |
that: |
| 327 |
- ansible_loop.nextitem is undefined |
| 328 |
- ansible_loop.index == 3 |
| 329 |
- ansible_loop.index0 == 2 |
| 330 |
- not ansible_loop.first |
| 331 |
- ansible_loop.last |
| 332 |
- ansible_loop.previtem == 'orange' |
| 333 |
- ansible_loop.allitems == ['apple', 'orange', 'banana'] |
| 334 |
- ansible_loop.revindex == 1 |
| 335 |
- ansible_loop.revindex0 == 0 |
| 336 |
- ansible_loop.length == 3 |
| 337 |
loop: |
| 338 |
- apple |
| 339 |
- orange |
| 340 |
- banana |
| 341 |
loop_control: |
| 342 |
extended: true |
| 343 |
when: item == 'banana' |
| 344 |
|
| 345 |
- name: Validate the loop_var name |
| 346 |
assert: |
| 347 |
that: |
| 348 |
- ansible_loop_var == 'alvin' |
| 349 |
loop: |
| 350 |
- 1 |
| 351 |
loop_control: |
| 352 |
loop_var: alvin |
| 353 |
|
| 354 |
# https://github.com/ansible/ansible/issues/58820 |
| 355 |
- name: Test using templated loop_var inside include_tasks |
| 356 |
include_tasks: templated_loop_var_tasks.yml |
| 357 |
loop: |
| 358 |
- value |
| 359 |
loop_control: |
| 360 |
loop_var: "{{ loop_var_name }}" |
| 361 |
vars: |
| 362 |
loop_var_name: templated_loop_var_name |
| 363 |
|
| 364 |
# https://github.com/ansible/ansible/issues/59414 |
| 365 |
- name: Test preserving original connection related vars |
| 366 |
debug: |
| 367 |
var: ansible_remote_tmp |
| 368 |
vars: |
| 369 |
ansible_remote_tmp: /tmp/test1 |
| 370 |
with_items: |
| 371 |
- 1 |
| 372 |
- 2 |
| 373 |
register: loop_out |
| 374 |
|
| 375 |
- assert: |
| 376 |
that: |
| 377 |
- loop_out['results'][1]['ansible_remote_tmp'] == '/tmp/test1' |
| 378 |
|
| 379 |
# https://github.com/ansible/ansible/issues/83619 |
| 380 |
- command: "echo {{ item }}" |
| 381 |
register: r |
| 382 |
loop: |
| 383 |
- changed |
| 384 |
- skipped |
| 385 |
loop_control: |
| 386 |
label: "{{ r.stdout }}" |
| 387 |
when: |
| 388 |
- item == "changed" |
| 389 |
|
| 390 |
- name: test that the second iteration result was skipped and generated a warning about the loop label |
| 391 |
assert: |
| 392 |
that: |
| 393 |
- r.results[1] is skipped |
| 394 |
- r.results[1].skip_reason is contains "result was False" |
| 395 |
- r.results[1].warnings | length == 1 |
| 396 |
- r.results[1].warnings[0] is contains "no attribute 'stdout'" |