| 1 |
- name: Failure |
| 2 |
set_fact: |
| 3 |
hello: world |
| 4 |
failed_when: true |
| 5 |
ignore_errors: yes |
| 6 |
register: intentional_failure |
| 7 |
|
| 8 |
- name: Success |
| 9 |
set_fact: |
| 10 |
hello: world |
| 11 |
register: intentional_success |
| 12 |
|
| 13 |
- name: Try failure test on non-dictionary |
| 14 |
set_fact: |
| 15 |
hello: "{{ 'nope' is failure }}" |
| 16 |
ignore_errors: yes |
| 17 |
register: misuse_of_failure |
| 18 |
|
| 19 |
- name: Assert failure tests work |
| 20 |
assert: |
| 21 |
that: |
| 22 |
- intentional_failure is failed # old name |
| 23 |
- intentional_failure is failure |
| 24 |
- intentional_success is not failure |
| 25 |
- misuse_of_failure is failed |
| 26 |
|
| 27 |
- name: Assert successful tests work |
| 28 |
assert: |
| 29 |
that: |
| 30 |
- intentional_success is succeeded # old name |
| 31 |
- intentional_success is success # old name |
| 32 |
- intentional_success is successful |
| 33 |
- intentional_failure is not successful |
| 34 |
|
| 35 |
- name: Try reachable host |
| 36 |
command: id |
| 37 |
register: reachable_host |
| 38 |
|
| 39 |
- name: Try unreachable host |
| 40 |
command: id |
| 41 |
delegate_to: unreachable |
| 42 |
ignore_unreachable: yes |
| 43 |
ignore_errors: yes |
| 44 |
register: unreachable_host |
| 45 |
|
| 46 |
- name: Try reachable test on non-dictionary |
| 47 |
set_fact: |
| 48 |
hello: "{{ 'nope' is reachable }}" |
| 49 |
ignore_errors: yes |
| 50 |
register: misuse_of_reachable |
| 51 |
|
| 52 |
- name: Assert reachable tests work |
| 53 |
assert: |
| 54 |
that: |
| 55 |
- misuse_of_reachable is failed |
| 56 |
- reachable_host is reachable |
| 57 |
- unreachable_host is not reachable |
| 58 |
|
| 59 |
- name: Try unreachable test on non-dictionary |
| 60 |
set_fact: |
| 61 |
hello: "{{ 'nope' is unreachable }}" |
| 62 |
ignore_errors: yes |
| 63 |
register: misuse_of_unreachable |
| 64 |
|
| 65 |
- name: Assert unreachable tests work |
| 66 |
assert: |
| 67 |
that: |
| 68 |
- misuse_of_unreachable is failed |
| 69 |
- reachable_host is not unreachable |
| 70 |
- unreachable_host is unreachable |
| 71 |
|
| 72 |
- name: Make changes |
| 73 |
file: |
| 74 |
path: dir_for_changed |
| 75 |
state: directory |
| 76 |
register: directory_created |
| 77 |
|
| 78 |
- name: Make no changes |
| 79 |
file: |
| 80 |
path: dir_for_changed |
| 81 |
state: directory |
| 82 |
register: directory_unchanged |
| 83 |
|
| 84 |
- name: Try changed test on non-dictionary |
| 85 |
set_fact: |
| 86 |
hello: "{{ 'nope' is changed }}" |
| 87 |
ignore_errors: yes |
| 88 |
register: misuse_of_changed |
| 89 |
|
| 90 |
# providing artificial task results since there are no modules in ansible-core that provide a 'results' list instead of 'changed' |
| 91 |
- name: Prepare artificial task results |
| 92 |
set_fact: |
| 93 |
results_all_changed: |
| 94 |
results: |
| 95 |
- changed: true |
| 96 |
- changed: true |
| 97 |
results_some_changed: |
| 98 |
results: |
| 99 |
- changed: true |
| 100 |
- changed: false |
| 101 |
results_none_changed: |
| 102 |
results: |
| 103 |
- changed: false |
| 104 |
- changed: false |
| 105 |
results_missing_changed: {} |
| 106 |
|
| 107 |
- name: Assert changed tests work |
| 108 |
assert: |
| 109 |
that: |
| 110 |
- directory_created is changed |
| 111 |
- directory_unchanged is not changed |
| 112 |
- misuse_of_changed is failed |
| 113 |
- results_all_changed is changed |
| 114 |
- results_some_changed is changed |
| 115 |
- results_none_changed is not changed |
| 116 |
- results_missing_changed is not changed |
| 117 |
|
| 118 |
- name: Skip me |
| 119 |
set_fact: |
| 120 |
hello: world |
| 121 |
when: false |
| 122 |
register: skipped_task |
| 123 |
|
| 124 |
- name: Don't skip me |
| 125 |
set_fact: |
| 126 |
hello: world |
| 127 |
register: executed_task |
| 128 |
|
| 129 |
- name: Skip me with multiple conditions |
| 130 |
set_fact: |
| 131 |
hello: world |
| 132 |
when: |
| 133 |
- True == True |
| 134 |
- foo == 'bar' |
| 135 |
vars: |
| 136 |
foo: foo |
| 137 |
register: skipped_task_multi_condition |
| 138 |
|
| 139 |
- name: Try skipped test on non-dictionary |
| 140 |
set_fact: |
| 141 |
hello: "{{ 'nope' is skipped }}" |
| 142 |
ignore_errors: yes |
| 143 |
register: misuse_of_skipped |
| 144 |
|
| 145 |
- name: Assert skipped tests work |
| 146 |
assert: |
| 147 |
that: |
| 148 |
- skipped_task is skipped |
| 149 |
- skipped_task.false_condition == False |
| 150 |
- executed_task is not skipped |
| 151 |
- misuse_of_skipped is failure |
| 152 |
- skipped_task_multi_condition is skipped |
| 153 |
- skipped_task_multi_condition.false_condition == "foo == 'bar'" |
| 154 |
|
| 155 |
- name: Not an async task |
| 156 |
set_fact: |
| 157 |
hello: world |
| 158 |
register: non_async_task |
| 159 |
|
| 160 |
- name: Complete an async task |
| 161 |
command: id |
| 162 |
async: 10 |
| 163 |
poll: 1 |
| 164 |
register: async_completed |
| 165 |
|
| 166 |
- name: Start an async task without waiting for completion |
| 167 |
shell: sleep 3 |
| 168 |
async: 10 |
| 169 |
poll: 0 |
| 170 |
register: async_incomplete |
| 171 |
|
| 172 |
- name: Try finished test on non-dictionary |
| 173 |
set_fact: |
| 174 |
hello: "{{ 'nope' is finished }}" |
| 175 |
ignore_errors: yes |
| 176 |
register: misuse_of_finished |
| 177 |
|
| 178 |
- name: Assert finished tests work (warning expected) |
| 179 |
assert: |
| 180 |
that: |
| 181 |
- non_async_task is finished |
| 182 |
- misuse_of_finished is failed |
| 183 |
- async_completed is finished |
| 184 |
- async_incomplete is not finished |
| 185 |
|
| 186 |
- name: Try started test on non-dictionary |
| 187 |
set_fact: |
| 188 |
hello: "{{ 'nope' is started }}" |
| 189 |
ignore_errors: yes |
| 190 |
register: misuse_of_started |
| 191 |
|
| 192 |
- name: Assert started tests work (warning expected) |
| 193 |
assert: |
| 194 |
that: |
| 195 |
- non_async_task is started |
| 196 |
- misuse_of_started is failed |
| 197 |
- async_completed is started |
| 198 |
- async_incomplete is started |
| 199 |
|
| 200 |
- name: Assert match tests work |
| 201 |
assert: |
| 202 |
that: |
| 203 |
- "'hello' is match('h.ll.')" |
| 204 |
- "'hello' is not match('.ll.')" |
| 205 |
|
| 206 |
- name: Assert search tests work |
| 207 |
assert: |
| 208 |
that: |
| 209 |
- "'hello' is search('.l')" |
| 210 |
- "'hello' is not search('nope')" |
| 211 |
|
| 212 |
- name: Assert regex tests work |
| 213 |
assert: |
| 214 |
that: |
| 215 |
- "'hello' is regex('.l')" |
| 216 |
- "'hello' is regex('.L', ignorecase=true)" |
| 217 |
- "'hello\nAnsible' is regex('^Ansible', multiline=true)" |
| 218 |
- "'hello' is not regex('.L')" |
| 219 |
- "'hello\nAnsible' is not regex('^Ansible')" |
| 220 |
|
| 221 |
- name: Try regex test with an invalid match_type |
| 222 |
set_fact: |
| 223 |
result: "{{ '1.0' is regex('.l', match_type='invalid') }}" |
| 224 |
ignore_errors: yes |
| 225 |
register: regex_invalid_match_type |
| 226 |
|
| 227 |
- name: Assert regex test raises an error when a match_type is invalid |
| 228 |
assert: |
| 229 |
that: |
| 230 |
- regex_invalid_match_type is failed |
| 231 |
- "'Invalid match_type specified.' in regex_invalid_match_type.msg" |
| 232 |
|
| 233 |
- name: Try version tests with bad operator |
| 234 |
set_fact: |
| 235 |
result: "{{ '1.0' is version('1.0', 'equals') }}" |
| 236 |
ignore_errors: yes |
| 237 |
register: version_bad_operator |
| 238 |
|
| 239 |
- name: Try version tests with bad value |
| 240 |
set_fact: |
| 241 |
result: "{{ '1.0' is version('nope', '==', true) }}" |
| 242 |
ignore_errors: yes |
| 243 |
register: version_bad_value |
| 244 |
|
| 245 |
- name: Try version with both strict and version_type |
| 246 |
debug: |
| 247 |
msg: "{{ '1.0' is version('1.0', strict=False, version_type='loose') }}" |
| 248 |
ignore_errors: yes |
| 249 |
register: version_strict_version_type |
| 250 |
|
| 251 |
- name: Try version with bad version_type |
| 252 |
debug: |
| 253 |
msg: "{{ '1.0' is version('1.0', version_type='boom') }}" |
| 254 |
ignore_errors: yes |
| 255 |
register: version_bad_version_type |
| 256 |
|
| 257 |
- name: Try version with bad semver |
| 258 |
debug: |
| 259 |
msg: "{{ 'nope' is version('nopenope', version_type='semver') }}" |
| 260 |
ignore_errors: yes |
| 261 |
register: version_bad_semver |
| 262 |
|
| 263 |
- name: Try version with empty input value |
| 264 |
debug: |
| 265 |
msg: "{{ '' is version('1.0', '>') }}" |
| 266 |
ignore_errors: yes |
| 267 |
register: version_empty_input |
| 268 |
|
| 269 |
- name: Try version with empty comparison value |
| 270 |
debug: |
| 271 |
msg: "{{ '1.0' is version('', '>') }}" |
| 272 |
ignore_errors: yes |
| 273 |
register: version_empty_comparison |
| 274 |
|
| 275 |
- name: Try version with empty input and comparison values |
| 276 |
debug: |
| 277 |
msg: "{{ '' is version('', '>') }}" |
| 278 |
ignore_errors: yes |
| 279 |
register: version_empty_both |
| 280 |
|
| 281 |
- name: Assert version tests work |
| 282 |
assert: |
| 283 |
that: |
| 284 |
- "'1.0' is version_compare('1.0', '==')" # old name |
| 285 |
- "'1.0' is version('1.0', '==')" |
| 286 |
- "'1.0' is version('2.0', '!=')" |
| 287 |
- "'1.0' is version('2.0', '<')" |
| 288 |
- "'2.0' is version('1.0', '>')" |
| 289 |
- "'1.0' is version('1.0', '<=')" |
| 290 |
- "'1.0' is version('1.0', '>=')" |
| 291 |
- "'1.0' is version_compare('1.0', '==', true)" # old name |
| 292 |
- "'1.0' is version('1.0', '==', true)" |
| 293 |
- "'1.0' is version('2.0', '!=', true)" |
| 294 |
- "'1.0' is version('2.0', '<', true)" |
| 295 |
- "'2.0' is version('1.0', '>', true)" |
| 296 |
- "'1.0' is version('1.0', '<=', true)" |
| 297 |
- "'1.0' is version('1.0', '>=', true)" |
| 298 |
- "'1.2.3' is version('2.0.0', 'lt', version_type='semver')" |
| 299 |
- "'2.14.0rc1' is version('2.14.0', 'lt', version_type='pep440')" |
| 300 |
- version_bad_operator is failed |
| 301 |
- version_bad_value is failed |
| 302 |
- version_strict_version_type is failed |
| 303 |
- version_bad_version_type is failed |
| 304 |
- version_bad_semver is failed |
| 305 |
- version_empty_input is failed |
| 306 |
- version_empty_input is search('version value cannot be empty') |
| 307 |
- version_empty_comparison is failed |
| 308 |
- version_empty_comparison is search('to compare against cannot be empty') |
| 309 |
- version_empty_both is failed |
| 310 |
- version_empty_both is search('version value cannot be empty') |
| 311 |
|
| 312 |
- name: Assert any tests work |
| 313 |
assert: |
| 314 |
that: |
| 315 |
- "[true, false] is any" |
| 316 |
- "[false] is not any" |
| 317 |
|
| 318 |
- name: Assert all tests work |
| 319 |
assert: |
| 320 |
that: |
| 321 |
- "[true] is all" |
| 322 |
- "[true, false] is not all" |
| 323 |
|
| 324 |
- name: Assert truthy tests work |
| 325 |
assert: |
| 326 |
that: |
| 327 |
- '"string" is truthy' |
| 328 |
- '"" is not truthy' |
| 329 |
- True is truthy |
| 330 |
- False is not truthy |
| 331 |
- true is truthy |
| 332 |
- false is not truthy |
| 333 |
- 1 is truthy |
| 334 |
- 0 is not truthy |
| 335 |
- '[""] is truthy' |
| 336 |
- '[] is not truthy' |
| 337 |
- '"on" is truthy(convert_bool=True)' |
| 338 |
- '"off" is not truthy(convert_bool=True)' |
| 339 |
- '"fred" is truthy(convert_bool=True)' |
| 340 |
- '{} is not truthy' |
| 341 |
- '{"key": "value"} is truthy' |
| 342 |
|
| 343 |
- name: Assert falsy tests work |
| 344 |
assert: |
| 345 |
that: |
| 346 |
- '"string" is not falsy' |
| 347 |
- '"" is falsy' |
| 348 |
- True is not falsy |
| 349 |
- False is falsy |
| 350 |
- true is not falsy |
| 351 |
- false is falsy |
| 352 |
- 1 is not falsy |
| 353 |
- 0 is falsy |
| 354 |
- '[""] is not falsy' |
| 355 |
- '[] is falsy' |
| 356 |
- '"on" is not falsy(convert_bool=True)' |
| 357 |
- '"off" is falsy(convert_bool=True)' |
| 358 |
- '{} is falsy' |
| 359 |
- '{"key": "value"} is not falsy' |
| 360 |
|
| 361 |
- name: Create vaulted variable for vault_encrypted test |
| 362 |
set_fact: |
| 363 |
vaulted_value: !vault | |
| 364 |
$ANSIBLE_VAULT;1.1;AES256 |
| 365 |
35323961353038346165643738646465376139363061353835303739663538343266303232326635 |
| 366 |
3365353662646236356665323135633630656238316530640a663362363763633436373439663031 |
| 367 |
33663433383037396438656464636433653837376361313638366362333037323961316364363363 |
| 368 |
3835616438623261650a636164376534376661393134326662326362323131373964313961623365 |
| 369 |
3833 |
| 370 |
|
| 371 |
- name: Assert vault_encrypted tests work |
| 372 |
assert: |
| 373 |
that: |
| 374 |
- vaulted_value is vault_encrypted |
| 375 |
- inventory_hostname is not vault_encrypted |
| 376 |
|
| 377 |
- name: Check vaulted_file test |
| 378 |
assert: |
| 379 |
that: |
| 380 |
- "'files/notvault' is not vaulted_file" |
| 381 |
- "'files/vault1' is vaulted_file" |
| 382 |
- "'files/vault2' is vaulted_file" |
| 383 |
|
| 384 |
|
| 385 |
- name: test timeout test |
| 386 |
tags: |
| 387 |
- timeout |
| 388 |
block: |
| 389 |
- command: sleep 5 |
| 390 |
timeout: 3 |
| 391 |
register: timed |
| 392 |
ignore_errors: true |
| 393 |
|
| 394 |
- assert: |
| 395 |
that: |
| 396 |
- timed is timedout |
| 397 |
- timed['timedout'].get('period', 0) == 3 |