back to listing index

297 - Ansible Conditional Guide - pad.yohdah.com

[web search]
Original source (pad.yohdah.com)
Tags: errors ansible
Clipped on: 2016-02-24

 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
# ansible conditionals

- name: do a kick flip
  shell: ./kickflip
  
  # when always true, will ./kickflip
  when: true == true

  # when always false, will not ./kickflip
  when: false == true

  # when two statements, will ./kickflip
  when: true == true and false == false

  # when two statements, will not ./kickflip
  when: true == false and false == false

  # when one of two statements, will ./kickflip
  when: false == true or false == false

  # when zero of two statements, will not ./kickflip
  when: false == true or false == true


# when: <a multi level variable> is defined or is not defined
# ansible will always raise an error and the issue isn't obvious.
#
# example fatal error:
#
#   fatal: [xxx.xxx.xxx.xxx] => error while evaluating conditional: levvvel_one.somevar not defined
#
# I know it's not defined, that's why I was testing ...
#
# When I am testing if something is defined, I don't expect it to be a fatal error when it isn't ...
#
# for example,
# 
- name: do a kick flip
  shell: ./kickflip

  # this is going to be a fatal error if level_one (key) isn't defined ...
  when: level_one.somevar is defined and level_one.somevar != 'potato'

  # this is a work around ... test the parent key first ...
  when: level_one is defined and level_one.somevar is defined and level_one.somevar != 'potato'