| 1 |
# Copyright: Contributors to the Ansible project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
- name: test with_sequence |
| 5 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 6 |
with_sequence: start=0 end=3 |
| 7 |
|
| 8 |
- name: test with_sequence backwards |
| 9 |
set_fact: "{{ 'y' + item }}={{ item }}" |
| 10 |
with_sequence: start=3 end=0 stride=-1 |
| 11 |
|
| 12 |
- name: verify with_sequence |
| 13 |
assert: |
| 14 |
that: |
| 15 |
- "x0 == '0'" |
| 16 |
- "x1 == '1'" |
| 17 |
- "x2 == '2'" |
| 18 |
- "x3 == '3'" |
| 19 |
- "y3 == '3'" |
| 20 |
- "y2 == '2'" |
| 21 |
- "y1 == '1'" |
| 22 |
- "y0 == '0'" |
| 23 |
|
| 24 |
- name: test with_sequence not failing on count == 0 |
| 25 |
debug: msg='previously failed with backward counting error' |
| 26 |
with_sequence: count=0 |
| 27 |
register: count_of_zero |
| 28 |
|
| 29 |
- name: test with_sequence does 1 when start == end |
| 30 |
debug: msg='should run once' |
| 31 |
with_sequence: start=1 end=1 |
| 32 |
register: start_equal_end |
| 33 |
|
| 34 |
- name: test with_sequence count 1 |
| 35 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 36 |
with_sequence: count=1 |
| 37 |
register: count_of_one |
| 38 |
|
| 39 |
- assert: |
| 40 |
that: |
| 41 |
- start_equal_end is not skipped |
| 42 |
- count_of_zero is skipped |
| 43 |
- count_of_one is not skipped |
| 44 |
|
| 45 |
- name: test with_sequence shortcut syntax (end) |
| 46 |
set_fact: "{{ 'ws_z_' + item }}={{ item }}" |
| 47 |
with_sequence: '4' |
| 48 |
|
| 49 |
- name: test with_sequence shortcut syntax (start-end/stride) |
| 50 |
set_fact: "{{ 'ws_z_' + item }}=stride_{{ item }}" |
| 51 |
with_sequence: '2-6/2' |
| 52 |
|
| 53 |
- name: test with_sequence shortcut syntax (start-end:format) |
| 54 |
set_fact: "{{ 'ws_z_' + item }}={{ item }}" |
| 55 |
with_sequence: '7-8:host%02d' |
| 56 |
|
| 57 |
- name: verify with_sequence shortcut syntax |
| 58 |
assert: |
| 59 |
that: |
| 60 |
- "ws_z_1 == '1'" |
| 61 |
- "ws_z_2 == 'stride_2'" |
| 62 |
- "ws_z_3 == '3'" |
| 63 |
- "ws_z_4 == 'stride_4'" |
| 64 |
- "ws_z_6 == 'stride_6'" |
| 65 |
- "ws_z_host07 == 'host07'" |
| 66 |
- "ws_z_host08 == 'host08'" |
| 67 |
|
| 68 |
- block: |
| 69 |
- name: EXPECTED FAILURE - test invalid arg |
| 70 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 71 |
with_sequence: start=0 junk=3 |
| 72 |
|
| 73 |
- fail: |
| 74 |
msg: "should not get here" |
| 75 |
rescue: |
| 76 |
- assert: |
| 77 |
that: |
| 78 |
- ansible_failed_task.name == "EXPECTED FAILURE - test invalid arg" |
| 79 |
- '"unrecognized arguments to with_sequence: " in ansible_failed_result.msg' |
| 80 |
- '"junk" in ansible_failed_result.msg' |
| 81 |
|
| 82 |
- block: |
| 83 |
- name: EXPECTED FAILURE - test bad kv value |
| 84 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 85 |
with_sequence: start=A end=3 |
| 86 |
|
| 87 |
- fail: |
| 88 |
msg: "should not get here" |
| 89 |
rescue: |
| 90 |
- assert: |
| 91 |
that: |
| 92 |
- ansible_failed_task.name == "EXPECTED FAILURE - test bad kv value" |
| 93 |
- ansible_failed_result.msg is contains "has an invalid value" |
| 94 |
|
| 95 |
- block: |
| 96 |
- name: EXPECTED FAILURE - test bad simple form start value |
| 97 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 98 |
with_sequence: A-4/2 |
| 99 |
|
| 100 |
- fail: |
| 101 |
msg: "should not get here" |
| 102 |
rescue: |
| 103 |
- assert: |
| 104 |
that: |
| 105 |
- ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form start value" |
| 106 |
- ansible_failed_result.msg is contains "has an invalid value" |
| 107 |
|
| 108 |
- block: |
| 109 |
- name: EXPECTED FAILURE - test bad simple form end value |
| 110 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 111 |
with_sequence: 1-B/2 |
| 112 |
|
| 113 |
- fail: |
| 114 |
msg: "should not get here" |
| 115 |
rescue: |
| 116 |
- assert: |
| 117 |
that: |
| 118 |
- ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form end value" |
| 119 |
- ansible_failed_result.msg is contains "has an invalid value" |
| 120 |
|
| 121 |
- block: |
| 122 |
- name: EXPECTED FAILURE - test bad simple form stride value |
| 123 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 124 |
with_sequence: 1-4/C |
| 125 |
|
| 126 |
- fail: |
| 127 |
msg: "should not get here" |
| 128 |
rescue: |
| 129 |
- assert: |
| 130 |
that: |
| 131 |
- ansible_failed_task.name == "EXPECTED FAILURE - test bad simple form stride value" |
| 132 |
- ansible_failed_result.msg is contains "has an invalid value" |
| 133 |
|
| 134 |
- block: |
| 135 |
- name: EXPECTED FAILURE - test no count or end |
| 136 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 137 |
with_sequence: start=1 |
| 138 |
|
| 139 |
- fail: |
| 140 |
msg: "should not get here" |
| 141 |
rescue: |
| 142 |
- assert: |
| 143 |
that: |
| 144 |
- ansible_failed_task.name == "EXPECTED FAILURE - test no count or end" |
| 145 |
- ansible_failed_result.msg is contains "must specify count or end in with_sequence" |
| 146 |
|
| 147 |
- block: |
| 148 |
- name: EXPECTED FAILURE - test both count and end |
| 149 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 150 |
with_sequence: start=1 end=4 count=2 |
| 151 |
|
| 152 |
- fail: |
| 153 |
msg: "should not get here" |
| 154 |
rescue: |
| 155 |
- assert: |
| 156 |
that: |
| 157 |
- ansible_failed_task.name == "EXPECTED FAILURE - test both count and end" |
| 158 |
- ansible_failed_result.msg is contains "can't specify both count and end in with_sequence" |
| 159 |
|
| 160 |
- block: |
| 161 |
- name: EXPECTED FAILURE - test count backwards message |
| 162 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 163 |
with_sequence: start=4 end=1 stride=2 |
| 164 |
|
| 165 |
- fail: |
| 166 |
msg: "should not get here" |
| 167 |
rescue: |
| 168 |
- assert: |
| 169 |
that: |
| 170 |
- ansible_failed_task.name == "EXPECTED FAILURE - test count backwards message" |
| 171 |
- ansible_failed_result.msg is contains "to count backwards make stride negative" |
| 172 |
|
| 173 |
- block: |
| 174 |
- name: EXPECTED FAILURE - test count forward message |
| 175 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 176 |
with_sequence: start=1 end=4 stride=-2 |
| 177 |
|
| 178 |
- fail: |
| 179 |
msg: "should not get here" |
| 180 |
rescue: |
| 181 |
- assert: |
| 182 |
that: |
| 183 |
- ansible_failed_task.name == "EXPECTED FAILURE - test count forward message" |
| 184 |
- ansible_failed_result.msg is contains "to count forward don't make stride negative" |
| 185 |
|
| 186 |
- block: |
| 187 |
- name: EXPECTED FAILURE - test bad format string message |
| 188 |
set_fact: "{{ 'x' + item }}={{ item }}" |
| 189 |
with_sequence: start=1 end=4 format=d |
| 190 |
|
| 191 |
- fail: |
| 192 |
msg: "should not get here" |
| 193 |
rescue: |
| 194 |
- assert: |
| 195 |
that: |
| 196 |
- ansible_failed_task.name == "EXPECTED FAILURE - test bad format string message" |
| 197 |
- ansible_failed_result.msg is contains expected |
| 198 |
vars: |
| 199 |
expected: "bad formatting string: d" |
| 200 |
|
| 201 |
# Tests for lookup()/plugin() jinja invocation: |
| 202 |
# Many of these tests check edge case behaviors that are only possible when invoking query/lookup sequence through jinja. |
| 203 |
# While they aren't particularly intuitive, these tests ensure playbooks that could be relying on these behaviors don't |
| 204 |
# break in future |
| 205 |
- name: Test lookup with keyword args only |
| 206 |
assert: |
| 207 |
that: |
| 208 |
- query("ansible.builtin.sequence", count=5, start=10) == ["10", "11", "12", "13", "14"] |
| 209 |
|
| 210 |
- name: Test that multiple positional args produces concatenated sequence |
| 211 |
assert: |
| 212 |
that: |
| 213 |
- query("ansible.builtin.sequence", "count=5 start=1", "count=3 start=10 stride=2") == ["1", "2", "3", "4", "5", "10", "12", "14"] |
| 214 |
|
| 215 |
- name: Test that keyword arguments are applied to all positional expressions |
| 216 |
assert: |
| 217 |
that: |
| 218 |
- query("ansible.builtin.sequence", "count=5 start=0", "count=5 start=20", stride=2) == ["0", "2", "4", "6", "8", "20", "22", "24", "26", "28"] |
| 219 |
|
| 220 |
- name: Test stride=0 produces an empty list |
| 221 |
assert: |
| 222 |
that: |
| 223 |
- query("ansible.builtin.sequence", "count=5 start=0", stride=0) == [] |
| 224 |
|
| 225 |
- name: Test stride=-1 produces an list of negative numbers |
| 226 |
assert: |
| 227 |
that: |
| 228 |
- query("ansible.builtin.sequence", "count=5 start=0", stride=-1) == ["0", "-1", "-2", "-3", "-4", "-5", "-6"] |
| 229 |
|
| 230 |
- name: Test that keyword arguments do not overwrite parameters present in positional expressions |
| 231 |
assert: |
| 232 |
that: |
| 233 |
- query("ansible.builtin.sequence", "count=5 start=0", "count=5", start=20) == ["0", "1", "2", "3", "4", "20", "21", "22", "23", "24"] |
| 234 |
|
| 235 |
- name: Test that call with no arguments produces an empty list |
| 236 |
assert: |
| 237 |
that: |
| 238 |
- query("ansible.builtin.sequence") == [] |