| 1 |
# (c) 2014, James Cammarata <[email protected]> |
| 2 |
# (c) 2019, Ansible Project |
| 3 |
|
| 4 |
- hosts: testhost |
| 5 |
gather_facts: false |
| 6 |
vars_files: |
| 7 |
- vars/main.yml |
| 8 |
tasks: |
| 9 |
- name: test conditional '==' |
| 10 |
shell: echo 'testing' |
| 11 |
when: 1 == 1 |
| 12 |
register: result |
| 13 |
|
| 14 |
- name: assert conditional '==' ran |
| 15 |
assert: |
| 16 |
that: |
| 17 |
- result is changed |
| 18 |
- "result.stdout == 'testing'" |
| 19 |
- "result.rc == 0" |
| 20 |
|
| 21 |
- name: test bad conditional '==' |
| 22 |
shell: echo 'testing' |
| 23 |
when: 0 == 1 |
| 24 |
register: result |
| 25 |
|
| 26 |
- name: assert bad conditional '==' did NOT run |
| 27 |
assert: |
| 28 |
that: |
| 29 |
- result is skipped |
| 30 |
|
| 31 |
- name: test conditional '!=' |
| 32 |
shell: echo 'testing' |
| 33 |
when: 0 != 1 |
| 34 |
register: result |
| 35 |
|
| 36 |
- name: assert conditional '!=' ran |
| 37 |
assert: |
| 38 |
that: |
| 39 |
- result is changed |
| 40 |
- "result.stdout == 'testing'" |
| 41 |
- "result.rc == 0" |
| 42 |
|
| 43 |
- name: test bad conditional '!=' |
| 44 |
shell: echo 'testing' |
| 45 |
when: 1 != 1 |
| 46 |
register: result |
| 47 |
|
| 48 |
- name: assert bad conditional '!=' did NOT run |
| 49 |
assert: |
| 50 |
that: |
| 51 |
- result is skipped |
| 52 |
|
| 53 |
- name: test conditional 'in' |
| 54 |
shell: echo 'testing' |
| 55 |
when: 1 in [1,2,3] |
| 56 |
register: result |
| 57 |
|
| 58 |
- name: assert conditional 'in' ran |
| 59 |
assert: |
| 60 |
that: |
| 61 |
- result is changed |
| 62 |
- "result.stdout == 'testing'" |
| 63 |
- "result.rc == 0" |
| 64 |
|
| 65 |
- name: test bad conditional 'in' |
| 66 |
shell: echo 'testing' |
| 67 |
when: 1 in [7,8,9] |
| 68 |
register: result |
| 69 |
|
| 70 |
- name: assert bad conditional 'in' did NOT run |
| 71 |
assert: |
| 72 |
that: |
| 73 |
- result is skipped |
| 74 |
|
| 75 |
- name: test conditional 'not in' |
| 76 |
shell: echo 'testing' |
| 77 |
when: 0 not in [1,2,3] |
| 78 |
register: result |
| 79 |
|
| 80 |
- name: assert conditional 'not in' ran |
| 81 |
assert: |
| 82 |
that: |
| 83 |
- result is changed |
| 84 |
- "result.stdout == 'testing'" |
| 85 |
- "result.rc == 0" |
| 86 |
|
| 87 |
- name: test bad conditional 'not in' |
| 88 |
shell: echo 'testing' |
| 89 |
when: 1 not in [1,2,3] |
| 90 |
register: result |
| 91 |
|
| 92 |
- name: assert bad conditional 'not in' did NOT run |
| 93 |
assert: |
| 94 |
that: |
| 95 |
- result is skipped |
| 96 |
|
| 97 |
- name: test conditional 'is defined' |
| 98 |
shell: echo 'testing' |
| 99 |
when: test_bare is defined |
| 100 |
register: result |
| 101 |
|
| 102 |
- name: assert conditional 'is defined' ran |
| 103 |
assert: |
| 104 |
that: |
| 105 |
- result is changed |
| 106 |
- "result.stdout == 'testing'" |
| 107 |
- "result.rc == 0" |
| 108 |
|
| 109 |
- name: test bad conditional 'is defined' |
| 110 |
shell: echo 'testing' |
| 111 |
when: foo_asdf_xyz is defined |
| 112 |
register: result |
| 113 |
|
| 114 |
- name: assert bad conditional 'is defined' did NOT run |
| 115 |
assert: |
| 116 |
that: |
| 117 |
- result is skipped |
| 118 |
|
| 119 |
- name: test conditional 'is not defined' |
| 120 |
shell: echo 'testing' |
| 121 |
when: foo_asdf_xyz is not defined |
| 122 |
register: result |
| 123 |
|
| 124 |
- name: assert conditional 'is not defined' ran |
| 125 |
assert: |
| 126 |
that: |
| 127 |
- result is changed |
| 128 |
- "result.stdout == 'testing'" |
| 129 |
- "result.rc == 0" |
| 130 |
|
| 131 |
- name: test bad conditional 'is not defined' |
| 132 |
shell: echo 'testing' |
| 133 |
when: test_bare is not defined |
| 134 |
register: result |
| 135 |
|
| 136 |
- name: assert bad conditional 'is not defined' did NOT run |
| 137 |
assert: |
| 138 |
that: |
| 139 |
- result is skipped |
| 140 |
|
| 141 |
- name: test bad conditional 'is undefined' |
| 142 |
shell: echo 'testing' |
| 143 |
when: test_bare is undefined |
| 144 |
register: result |
| 145 |
|
| 146 |
- name: assert bad conditional 'is undefined' did NOT run |
| 147 |
assert: |
| 148 |
that: |
| 149 |
- result is skipped |
| 150 |
|
| 151 |
- name: test bare conditional |
| 152 |
shell: echo 'testing' |
| 153 |
when: test_bare |
| 154 |
register: result |
| 155 |
|
| 156 |
- name: assert bare conditional ran |
| 157 |
assert: |
| 158 |
that: |
| 159 |
- result is changed |
| 160 |
- "result.stdout == 'testing'" |
| 161 |
- "result.rc == 0" |
| 162 |
|
| 163 |
- name: not test bare conditional |
| 164 |
shell: echo 'testing' |
| 165 |
when: not test_bare |
| 166 |
register: result |
| 167 |
|
| 168 |
- name: assert did not run |
| 169 |
assert: |
| 170 |
that: |
| 171 |
- result is skipped |
| 172 |
|
| 173 |
- name: not empty string is true |
| 174 |
shell: echo 'testing' |
| 175 |
when: not string_lit_empty |
| 176 |
register: result |
| 177 |
|
| 178 |
- name: assert ran |
| 179 |
assert: |
| 180 |
that: |
| 181 |
- result is not skipped |
| 182 |
|
| 183 |
- name: not literal 0 is true |
| 184 |
shell: echo 'testing' |
| 185 |
when: not int_lit_0 |
| 186 |
register: result |
| 187 |
|
| 188 |
- name: assert ran |
| 189 |
assert: |
| 190 |
that: |
| 191 |
- result is not skipped |
| 192 |
|
| 193 |
- name: not literal 1 is false |
| 194 |
shell: echo 'testing' |
| 195 |
when: not int_lit_1 |
| 196 |
register: result |
| 197 |
|
| 198 |
- name: assert did not run |
| 199 |
assert: |
| 200 |
that: |
| 201 |
- result is skipped |
| 202 |
|
| 203 |
- name: not literal string "true" is false |
| 204 |
shell: echo 'testing' |
| 205 |
when: not string_lit_true |
| 206 |
register: result |
| 207 |
|
| 208 |
- name: assert did not run |
| 209 |
assert: |
| 210 |
that: |
| 211 |
- result is skipped |
| 212 |
|
| 213 |
- name: not literal string "false" is false |
| 214 |
shell: echo 'testing' |
| 215 |
when: not string_lit_false |
| 216 |
register: result |
| 217 |
|
| 218 |
- name: assert did not run |
| 219 |
assert: |
| 220 |
that: |
| 221 |
- result is skipped |
| 222 |
|
| 223 |
- name: not literal string "true" is false |
| 224 |
shell: echo 'testing' |
| 225 |
when: not string_lit_true |
| 226 |
register: result |
| 227 |
|
| 228 |
- name: assert did not run |
| 229 |
assert: |
| 230 |
that: |
| 231 |
- result is skipped |
| 232 |
|
| 233 |
- name: test conditional using a variable |
| 234 |
shell: echo 'testing' |
| 235 |
when: test_bare_var == 123 |
| 236 |
register: result |
| 237 |
|
| 238 |
- name: assert conditional using a variable ran |
| 239 |
assert: |
| 240 |
that: |
| 241 |
- result is changed |
| 242 |
- "result.stdout == 'testing'" |
| 243 |
- "result.rc == 0" |
| 244 |
|
| 245 |
- name: test bad conditional based on nested variables with bool filter |
| 246 |
shell: echo 'testing' |
| 247 |
when: test_bare_nested_bad|bool |
| 248 |
register: result |
| 249 |
|
| 250 |
- name: assert that the bad nested conditional did NOT run as bool forces evaluation |
| 251 |
assert: |
| 252 |
that: |
| 253 |
- result is skipped |
| 254 |
|
| 255 |
|
| 256 |
#----------------------------------------------------------------------- |
| 257 |
# proper booleanification tests (issue #8629) |
| 258 |
|
| 259 |
- name: set fact to string 'false' |
| 260 |
set_fact: bool_test1=false |
| 261 |
|
| 262 |
- name: set fact to string 'False' |
| 263 |
set_fact: bool_test2=False |
| 264 |
|
| 265 |
- name: set fact to a proper boolean using complex args |
| 266 |
set_fact: |
| 267 |
bool_test3: false |
| 268 |
|
| 269 |
- name: "test proper boolean value of complex arg using 'when: var'" |
| 270 |
command: echo 'hi' |
| 271 |
when: bool_test3 |
| 272 |
register: result |
| 273 |
|
| 274 |
- name: assert that the task did not run for proper boolean false |
| 275 |
assert: |
| 276 |
that: |
| 277 |
- result is skipped |
| 278 |
|
| 279 |
- name: "test proper boolean value of complex arg using 'when: not var'" |
| 280 |
command: echo 'hi' |
| 281 |
when: not bool_test3 |
| 282 |
register: result |
| 283 |
|
| 284 |
- name: assert that the task DID run for not false |
| 285 |
assert: |
| 286 |
that: |
| 287 |
- result is changed |
| 288 |
|
| 289 |
- set_fact: skipped_bad_attribute=True |
| 290 |
- block: |
| 291 |
- name: test a with_items loop using a variable with a missing attribute |
| 292 |
debug: var=item |
| 293 |
with_items: "{{cond_bad_attribute.results | default('')}}" |
| 294 |
register: result |
| 295 |
- set_fact: skipped_bad_attribute=False |
| 296 |
- name: assert the task was skipped |
| 297 |
assert: |
| 298 |
that: |
| 299 |
- skipped_bad_attribute |
| 300 |
when: cond_bad_attribute is defined and 'results' in cond_bad_attribute |
| 301 |
|
| 302 |
- name: test a with_items loop skipping a single item |
| 303 |
debug: var=item |
| 304 |
with_items: "{{cond_list_of_items.results}}" |
| 305 |
when: item != 'b' |
| 306 |
register: result |
| 307 |
|
| 308 |
- debug: var=result |
| 309 |
|
| 310 |
- name: assert only a single item was skipped |
| 311 |
assert: |
| 312 |
that: |
| 313 |
- result.results|length == 3 |
| 314 |
- result.results[1].skipped |
| 315 |
|
| 316 |
- name: test complex templated condition |
| 317 |
debug: msg="it works" |
| 318 |
when: vars_file_var in things1|union([vars_file_var]) |
| 319 |
|
| 320 |
- name: test dict with invalid key is undefined |
| 321 |
vars: |
| 322 |
mydict: |
| 323 |
a: foo |
| 324 |
b: bar |
| 325 |
debug: var=mydict['c'] |
| 326 |
register: result |
| 327 |
when: mydict['c'] is undefined |
| 328 |
|
| 329 |
- name: assert the task did not fail |
| 330 |
assert: |
| 331 |
that: |
| 332 |
- result is success |
| 333 |
|
| 334 |
- name: test dict with invalid key does not run with conditional is defined |
| 335 |
vars: |
| 336 |
mydict: |
| 337 |
a: foo |
| 338 |
b: bar |
| 339 |
debug: var=mydict['c'] |
| 340 |
when: mydict['c'] is defined |
| 341 |
register: result |
| 342 |
|
| 343 |
- name: assert the task was skipped |
| 344 |
assert: |
| 345 |
that: |
| 346 |
- result is skipped |
| 347 |
|
| 348 |
- name: test list with invalid element does not run with conditional is defined |
| 349 |
vars: |
| 350 |
mylist: [] |
| 351 |
debug: var=mylist[0] |
| 352 |
when: mylist[0] is defined |
| 353 |
register: result |
| 354 |
|
| 355 |
- name: assert the task was skipped |
| 356 |
assert: |
| 357 |
that: |
| 358 |
- result is skipped |
| 359 |
|
| 360 |
- name: test list with invalid element is undefined |
| 361 |
vars: |
| 362 |
mylist: [] |
| 363 |
debug: var=mylist[0] |
| 364 |
when: mylist[0] is undefined |
| 365 |
register: result |
| 366 |
|
| 367 |
- name: assert the task did not fail |
| 368 |
assert: |
| 369 |
that: |
| 370 |
- result is success |
| 371 |
|
| 372 |
|
| 373 |
- name: Deal with multivar equality |
| 374 |
tags: ['leveldiff'] |
| 375 |
vars: |
| 376 |
toplevel_hash: |
| 377 |
hash_var_one: justastring |
| 378 |
hash_var_two: something.with.dots |
| 379 |
hash_var_three: something:with:colons |
| 380 |
hash_var_four: something/with/slashes |
| 381 |
hash_var_five: something with spaces |
| 382 |
hash_var_six: yes |
| 383 |
hash_var_seven: no |
| 384 |
toplevel_var_one: justastring |
| 385 |
toplevel_var_two: something.with.dots |
| 386 |
toplevel_var_three: something:with:colons |
| 387 |
toplevel_var_four: something/with/slashes |
| 388 |
toplevel_var_five: something with spaces |
| 389 |
toplevel_var_six: yes |
| 390 |
toplevel_var_seven: no |
| 391 |
block: |
| 392 |
|
| 393 |
|
| 394 |
- name: var subkey with 'yes' value |
| 395 |
debug: |
| 396 |
var: toplevel_hash.hash_var_six |
| 397 |
register: sub |
| 398 |
when: toplevel_hash.hash_var_six |
| 399 |
|
| 400 |
- debug: |
| 401 |
var: toplevel_var_six |
| 402 |
register: top |
| 403 |
when: toplevel_var_six |
| 404 |
|
| 405 |
- name: ensure top and multi work same |
| 406 |
assert: |
| 407 |
that: |
| 408 |
- top is not skipped |
| 409 |
- sub is not skipped |
| 410 |
|
| 411 |
- name: var subkey with 'no' value |
| 412 |
debug: |
| 413 |
var: toplevel_hash.hash_var_seven |
| 414 |
register: sub |
| 415 |
when: toplevel_hash.hash_var_seven |
| 416 |
|
| 417 |
- debug: |
| 418 |
var: toplevel_var_seven |
| 419 |
register: top |
| 420 |
when: toplevel_var_seven |
| 421 |
|
| 422 |
- name: ensure top and multi work same |
| 423 |
assert: |
| 424 |
that: |
| 425 |
- top is skipped |
| 426 |
- sub is skipped |
| 427 |
|
| 428 |
# # the feature that allowed this functionality (bare vars in conditionals) was removed in 2.12 |
| 429 |
# # preserved/augmented tests to verify it stays broken... |
| 430 |
# - name: test that non-truthy expression (no longer) fails with with_items (non-boolean result warning) |
| 431 |
# assert: |
| 432 |
# that: |
| 433 |
# - item |
| 434 |
# with_items: |
| 435 |
# - 1 == 0 |
| 436 |
# |
| 437 |
# # the feature that allowed this functionality (bare vars in conditionals) was removed in 2.12 |
| 438 |
# # preserved/augmented tests to verify it stays broken... |
| 439 |
# - name: test that non-truthy expression (no longer) fails with loop (non-boolean result warning) |
| 440 |
# assert: |
| 441 |
# that: |
| 442 |
# - item |
| 443 |
# loop: |
| 444 |
# - 1 == 0 |
| 445 |
|
| 446 |
|
| 447 |
- set_fact: |
| 448 |
sentinel_file: '{{ lookup("env", "OUTPUT_DIR")}}/LOOKUP_SIDE_EFFECT.txt' |
| 449 |
|
| 450 |
- name: ensure sentinel file is absent |
| 451 |
file: |
| 452 |
path: '{{ sentinel_file }}' |
| 453 |
state: absent |
| 454 |
- name: get an untrusted var that's a valid Jinja expression with a side-effect |
| 455 |
shell: | |
| 456 |
echo "lookup('pipe', 'echo bang > \"$SENTINEL_FILE\" && cat \"$SENTINEL_FILE\"')" |
| 457 |
environment: |
| 458 |
SENTINEL_FILE: '{{ sentinel_file }}' |
| 459 |
register: untrusted_expr |
| 460 |
- name: use a conditional with an inline template that refers to the untrusted expression |
| 461 |
debug: |
| 462 |
msg: look at some seemingly innocuous stuff |
| 463 |
when: '"foo" in {{ untrusted_expr.stdout }}' |
| 464 |
ignore_errors: true |
| 465 |
- name: ensure the untrusted expression side-effect has not executed |
| 466 |
stat: |
| 467 |
path: '{{ sentinel_file }}' |
| 468 |
register: sentinel_stat |
| 469 |
- assert: |
| 470 |
that: |
| 471 |
- not sentinel_stat.stat.exists |
| 472 |
|
| 473 |
- name: use a conditional with an untrusted string result |
| 474 |
assert: |
| 475 |
that: "{{ '1 == 1' }}" |
| 476 |
ignore_errors: yes |
| 477 |
register: untrusted_cond |
| 478 |
|
| 479 |
- assert: |
| 480 |
that: |
| 481 |
- untrusted_cond is failed |
| 482 |
- untrusted_cond.msg is contains "untrusted template or expression" |