FreeKB - Ansible when parameter (if else statement)
Ansible - when parameter (if else statement)

The when parameter (which is like an if statement) can be used to do something when a condition evaluates to true or false.

when: <some condition>

 

Often, the when parameter is used to compare a variable to a boolean (such as true, false, 1, or 0), to a string (such as Hello World), or to an integer (such as 12345). In this example, the when parameter is used to determine if the foo variable contains string "bar". Notice foo is not wrapped in quotes and bar is wrapped in quotes. Since foo is not wrapped in quotes, foo is interpreted as a variable. Because bar is wrapped in quotes, bar is interpreted as a string.

- debug: 
    msg: the foo variable does not equal the bar string
  when: foo != 'bar'

 

The when parameter can also be used to compare a variable to another variable. In this example, the foo variable is compared to the bar variable. Notice both foo and bar are not wrapped in quotes, meaning that both will be interpreted as variables.

- debug: 
    msg: the foo variable does not equal the bar variable
  when: foo != bar

 

Or, you may want to convert a variable to a boolean, integer, or string. In this example, the bar variable is converted into a string.

- debug: 
    msg: the foo variable does not equal the bar variable converted into a string
  when: foo != bar|string

 

Multiple when statements can be used like this.

- debug: 
    msg: multiple when statements
  when: foo != hello and foo != world

 

Or like this, using or.

- debug: 
    msg: multiple when statements
  when: foo != hello or foo != world

 

Or like this. This would perform an "and" situation, where all of the when statements would have to evaluate to true, not an "or" situation.

- debug: 
    msg: multiple when statements
  when:
    - foo != hello
    - foo != world

 

Or like this, using or.

- debug: 
    msg: multiple when statements
  when:
    - foo != hello or
    - foo != world

 

Sometimes, for more complex situation, or to avoid multiple tasks, using if else statement like this might be OK.

- set_fact:
  foo: "{% if foo == 'hello' %}Hello{% elif foo == 'world' %}World{% else %}false{% endif %}"

 

This table lists common when statements.

when the foo variable is defined or undefined

when: foo is defined

when: foo is undefined

when the foo variable or array is empty or not empty

when: foo | length > 0

when: foo | length == 0

when the foo variable does or does not equal string hello or world

when: foo == 'hello'

when: foo != 'world'

when the foo variable does or does not contain an integer

when: foo | type_debug == 'int'

when: foo | type_debug != 'int'

when the foo variable does or does not contain a boolean

when: foo | type_debug == 'bool'

when: foo | type_debug != 'bool'

when the foo variable does or does not equal boolean true or false

when: foo == true

when: foo != false

when the foo variable or array does or does not contain string hello or world

when: foo is search 'hello|world'

when: foo is not search 'hello|world'

when the content of the foo variable does or does not contain the content of the bar variable

when: bar in foo

when: bar not in foo

when the foo variable or array does or does not contain boolean true

when: true in foo

when: false not in foo

when the foo variable does or does not contain string hello or world with no whitespace

when: foo is regex '^(hello|world)$'

when: foo is not regex '^(hello|world)$'

when the foo variable does contain string hello

when: foo | regex_search('.*hello.*')

when hostname in one or more groups when: inventory_hostname in ( groups['foo'] + groups['bar'] )

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter ffcc4 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |