Debian Codegraph @

test/integration/targets/register_projections/tasks/keywords/when.yml

raw
1 - name: Use register projections in 'when' without a loop
2 debug:
3 # A non-loop task cannot use registered values in 'when', since register occurs after the action runs.
4 when: when_rp | default(1) == 1
5 register:
6 when_rp: "2"
7 when_result: _task.result
8
9 - name: Verify results
10 assert:
11 that:
12 - when_result.msg == "Hello world!"
13
14 - name: Use register projections in 'when' with a loop
15 debug:
16 loop: [1, 2, 3, 4, 5, 6]
17 when: when_loop_rp | default(0) in (1, 3)
18 register:
19 when_loop_rp: _task.loop_result.results | length
20 when_loop_rp_results: _task.loop_result.results
21 when_loop_rp_item: item
22
23 - name: Verify results
24 assert:
25 that:
26 - when_loop_rp == 6
27 - when_loop_rp_item == 6
28 # Results are recorded after the action runs, but 'when' is evaluated before.
29 # That means the 2nd and 4th loop items are executed, since 'when' does not see the results from the current item.
30 - when_loop_rp_results[0] is skipped
31 - when_loop_rp_results[1] is not skipped
32 - when_loop_rp_results[2] is skipped
33 - when_loop_rp_results[3] is not skipped
34 - when_loop_rp_results[4] is skipped
35 - when_loop_rp_results[5] is skipped
36
37 - name: Use '_task.result' in 'when' with a loop on the first item
38 debug:
39 loop: [1, 2, 3]
40 # On the first loop iteration, '_task.result' is not yet available, so a default needs to be provided.
41 when: |
42 (_task.result | default({"msg": "First!"})).get("msg") != "First!"
43 register: result
44
45 - name: Verify the result
46 assert:
47 that:
48 - result.results | length == 3
49 - result.results[0] is skipped
50 - result.results[1] is not skipped
51 - result.results[2] is not skipped
52
53 - name: Use '_task.result' in 'when' with a loop to access the previous item
54 debug:
55 loop: [1, 2, 3, 4, 5]
56 # This 'when' expression consults the previous item's results.
57 # As a result, alternating items are skipped, starting with the first item.
58 when: _task.result | default({}) is skipped
59 register: result
60
61 - name: Verify the result
62 assert:
63 that:
64 - result.results | length == 5
65 - result.results[0] is skipped
66 - result.results[1] is not skipped
67 - result.results[2] is skipped
68 - result.results[3] is not skipped
69 - result.results[4] is skipped