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