summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/conditionals/play.yml
blob: 455818c97c08e0a93a9da69b56c310e168c127ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
# (c) 2014, James Cammarata <jcammarata@ansible.com>
# (c) 2019, Ansible Project

- hosts: testhost
  gather_facts: false
  vars_files:
    - vars/main.yml
  tasks:
    - name: test conditional '=='
      shell: echo 'testing'
      when: 1 == 1
      register: result

    - name: assert conditional '==' ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional '=='
      shell: echo 'testing'
      when: 0 == 1
      register: result

    - name: assert bad conditional '==' did NOT run
      assert:
        that:
        - result is skipped

    - name: test conditional '!='
      shell: echo 'testing'
      when: 0 != 1
      register: result

    - name: assert conditional '!=' ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional '!='
      shell: echo 'testing'
      when: 1 != 1
      register: result

    - name: assert bad conditional '!=' did NOT run
      assert:
        that:
        - result is skipped

    - name: test conditional 'in'
      shell: echo 'testing'
      when: 1 in [1,2,3]
      register: result

    - name: assert conditional 'in' ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional 'in'
      shell: echo 'testing'
      when: 1 in [7,8,9]
      register: result

    - name: assert bad conditional 'in' did NOT run
      assert:
        that:
        - result is skipped

    - name: test conditional 'not in'
      shell: echo 'testing'
      when: 0 not in [1,2,3]
      register: result

    - name: assert conditional 'not in' ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional 'not in'
      shell: echo 'testing'
      when: 1 not in [1,2,3]
      register: result

    - name: assert bad conditional 'not in' did NOT run
      assert:
        that:
        - result is skipped

    - name: test conditional 'is defined'
      shell: echo 'testing'
      when: test_bare is defined
      register: result

    - name: assert conditional 'is defined' ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional 'is defined'
      shell: echo 'testing'
      when: foo_asdf_xyz is defined
      register: result

    - name: assert bad conditional 'is defined' did NOT run
      assert:
        that:
        - result is skipped

    - name: test conditional 'is not defined'
      shell: echo 'testing'
      when: foo_asdf_xyz is not defined
      register: result

    - name: assert conditional 'is not defined' ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional 'is not defined'
      shell: echo 'testing'
      when: test_bare is not defined
      register: result

    - name: assert bad conditional 'is not defined' did NOT run
      assert:
        that:
        - result is skipped

    - name: test bad conditional 'is undefined'
      shell: echo 'testing'
      when: test_bare is undefined
      register: result

    - name: assert bad conditional 'is undefined' did NOT run
      assert:
        that:
        - result is skipped

    - name: test bare conditional
      shell: echo 'testing'
      when: test_bare
      register: result

    - name: assert bare conditional ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: not test bare conditional
      shell: echo 'testing'
      when: not test_bare
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: empty string is false
      shell: echo 'testing'
      when: string_lit_empty
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: not empty string is true
      shell: echo 'testing'
      when: not string_lit_empty
      register: result

    - name: assert ran
      assert:
        that:
        - result is not skipped

    - name: literal 0 is false
      shell: echo 'testing'
      when: int_lit_0
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: not literal 0 is true
      shell: echo 'testing'
      when: not int_lit_0
      register: result

    - name: assert ran
      assert:
        that:
        - result is not skipped

    - name: literal 1 is true
      shell: echo 'testing'
      when: int_lit_1
      register: result

    - name: assert ran
      assert:
        that:
        - result is not skipped

    - name: not literal 1 is false
      shell: echo 'testing'
      when: not int_lit_1
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: null is false
      shell: echo 'testing'
      when: lit_null
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: literal string "true" is true
      shell: echo 'testing'
      when: string_lit_true
      register: result

    - name: assert ran
      assert:
        that:
        - result is not skipped

    - name: not literal string "true" is false
      shell: echo 'testing'
      when: not string_lit_true
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: literal string "false" is true (nonempty string)
      shell: echo 'testing'
      when: string_lit_false
      register: result

    - name: assert ran
      assert:
        that:
        - result is not skipped

    - name: not literal string "false" is false
      shell: echo 'testing'
      when: not string_lit_false
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: not literal string "true" is false
      shell: echo 'testing'
      when: not string_lit_true
      register: result

    - name: assert did not run
      assert:
        that:
        - result is skipped

    - name: test conditional using a variable
      shell: echo 'testing'
      when: test_bare_var == 123
      register: result

    - name: assert conditional using a variable ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test good conditional based on nested variables
      shell: echo 'testing'
      when: test_bare_nested_good
      register: result

    - name: assert good conditional based on nested var ran
      assert:
        that:
        - result is changed
        - "result.stdout == 'testing'"
        - "result.rc == 0"

    - name: test bad conditional based on nested variables
      shell: echo 'testing'
      when: test_bare_nested_bad
      register: result

    - debug: var={{item}}
      loop:
        - result
        - test_bare_nested_bad

    - name: assert that the bad nested conditional ran (it is a non-empty string, so truthy)
      assert:
        that:
         - result is not skipped

    - name: test bad conditional based on nested variables with bool filter
      shell: echo 'testing'
      when: test_bare_nested_bad|bool
      register: result

    - name: assert that the bad nested conditional did NOT run as bool forces evaluation
      assert:
        that:
        - result is skipped


    #-----------------------------------------------------------------------
    # proper booleanification tests (issue #8629)

    - name: set fact to string 'false'
      set_fact: bool_test1=false

    - name: set fact to string 'False'
      set_fact: bool_test2=False

    - name: set fact to a proper boolean using complex args
      set_fact:
        bool_test3: false

    - name: "test boolean value 'false' string using 'when: var'"
      command: echo 'hi'
      when: bool_test1
      register: result

    - name: assert that the task did not run for 'false'
      assert:
        that:
        - result is skipped

    - name: "test boolean value 'false' string using 'when: not var'"
      command: echo 'hi'
      when: not bool_test1
      register: result

    - name: assert that the task DID run for not 'false'
      assert:
        that:
        - result is changed

    - name: "test boolean value of 'False' string using 'when: var'"
      command: echo 'hi'
      when: bool_test2
      register: result

    - name: assert that the task did not run for 'False'
      assert:
        that:
        - result is skipped

    - name: "test boolean value 'False' string using 'when: not var'"
      command: echo 'hi'
      when: not bool_test2
      register: result

    - name: assert that the task DID run for not 'False'
      assert:
        that:
        - result is changed

    - name: "test proper boolean value of complex arg using 'when: var'"
      command: echo 'hi'
      when: bool_test3
      register: result

    - name: assert that the task did not run for proper boolean false
      assert:
        that:
        - result is skipped

    - name: "test proper boolean value of complex arg using 'when: not var'"
      command: echo 'hi'
      when: not bool_test3
      register: result

    - name: assert that the task DID run for not false
      assert:
        that:
        - result is changed

    - set_fact: skipped_bad_attribute=True
    - block:
      - name: test a with_items loop using a variable with a missing attribute
        debug: var=item
        with_items: "{{cond_bad_attribute.results | default('')}}"
        register: result
      - set_fact: skipped_bad_attribute=False
      - name: assert the task was skipped
        assert:
          that:
          - skipped_bad_attribute
      when: cond_bad_attribute is defined and 'results' in cond_bad_attribute

    - name: test a with_items loop skipping a single item
      debug: var=item
      with_items: "{{cond_list_of_items.results}}"
      when: item != 'b'
      register: result

    - debug: var=result

    - name: assert only a single item was skipped
      assert:
        that:
        - result.results|length == 3
        - result.results[1].skipped

    - name: test complex templated condition
      debug: msg="it works"
      when: vars_file_var in things1|union([vars_file_var])

    - name: test dict with invalid key is undefined
      vars:
        mydict:
         a: foo
         b: bar
      debug: var=mydict['c']
      register: result
      when: mydict['c'] is undefined

    - name: assert the task did not fail
      assert:
        that:
          - result is success

    - name: test dict with invalid key does not run with conditional is defined
      vars:
        mydict:
          a: foo
          b: bar
      debug: var=mydict['c']
      when: mydict['c'] is defined
      register: result

    - name: assert the task was skipped
      assert:
        that:
          - result is skipped

    - name: test list with invalid element does not run with conditional is defined
      vars:
        mylist: []
      debug: var=mylist[0]
      when: mylist[0] is defined
      register: result

    - name: assert the task was skipped
      assert:
        that:
          - result is skipped

    - name: test list with invalid element is undefined
      vars:
        mylist: []
      debug: var=mylist[0]
      when: mylist[0] is undefined
      register: result

    - name: assert the task did not fail
      assert:
        that:
          - result is success


    - name: Deal with multivar equality
      tags: ['leveldiff']
      vars:
          toplevel_hash:
            hash_var_one: justastring
            hash_var_two: something.with.dots
            hash_var_three: something:with:colons
            hash_var_four: something/with/slashes
            hash_var_five: something with spaces
            hash_var_six: yes
            hash_var_seven: no
          toplevel_var_one: justastring
          toplevel_var_two: something.with.dots
          toplevel_var_three: something:with:colons
          toplevel_var_four: something/with/slashes
          toplevel_var_five: something with spaces
          toplevel_var_six: yes
          toplevel_var_seven: no
      block:

      - name: var subkey simple string
        debug:
          var: toplevel_hash.hash_var_one
        register: sub
        when: toplevel_hash.hash_var_one

      - name: toplevel simple string
        debug:
          var: toplevel_var_one
        when: toplevel_var_one
        register: top
        ignore_errors: yes

      - name: ensure top and multi work same
        assert:
          that:
            - top is not skipped
            - sub is not skipped
            - top is not failed
            - sub is not failed

      - name: var subkey string with dots
        debug:
          var: toplevel_hash.hash_var_two
        register: sub
        when: toplevel_hash.hash_var_two

      - debug:
          var: toplevel_var_two
        when: toplevel_var_two
        register: top
        ignore_errors: yes

      - name: ensure top and multi work same
        assert:
          that:
            - top is not skipped
            - sub is not skipped
            - top is not failed
            - sub is not failed

      - name: var subkey string with dots
        debug:
          var: toplevel_hash.hash_var_three
        register: sub
        when: toplevel_hash.hash_var_three

      - debug:
          var: toplevel_var_three
        when: toplevel_var_three
        register: top
        ignore_errors: yes

      - name: ensure top and multi work same
        assert:
          that:
            - top is not skipped
            - sub is not skipped
            - top is not failed
            - sub is not failed

      - name: var subkey string with colon
        debug:
          var: toplevel_hash.hash_var_four
        register: sub
        when: toplevel_hash.hash_var_four

      - debug:
          var: toplevel_var_four
        when: toplevel_var_four
        register: top
        ignore_errors: yes

      - name: ensure top and multi work same
        assert:
          that:
            - top is not skipped
            - sub is not skipped
            - top is not failed
            - sub is not failed

      - name: var subkey string with spaces
        debug:
          var: toplevel_hash.hash_var_five
        register: sub
        when: toplevel_hash.hash_var_five

      - debug:
          var: toplevel_var_five
        when: toplevel_var_five
        register: top
        ignore_errors: yes

      - name: ensure top and multi work same
        assert:
          that:
            - top is not skipped
            - sub is not skipped
            - top is not failed
            - sub is not failed

      - name: var subkey with 'yes' value
        debug:
          var: toplevel_hash.hash_var_six
        register: sub
        when: toplevel_hash.hash_var_six

      - debug:
          var: toplevel_var_six
        register: top
        when: toplevel_var_six

      - name: ensure top and multi work same
        assert:
          that:
            - top is not skipped
            - sub is not skipped

      - name: var subkey with 'no' value
        debug:
          var: toplevel_hash.hash_var_seven
        register: sub
        when: toplevel_hash.hash_var_seven

      - debug:
          var: toplevel_var_seven
        register: top
        when: toplevel_var_seven

      - name: ensure top and multi work same
        assert:
          that:
            - top is skipped
            - sub is skipped

      - name: test that 'comparison expression' item works with_items
        assert:
          that:
            - item
        with_items:
          - 1 == 1

      - name: test that 'comparison expression' item works in loop
        assert:
          that:
            - item
        loop:
          - 1 == 1