How to define multiple when conditions in Ansible

ww‮ual.w‬tturi.com
How to define multiple when conditions in Ansible

In Ansible, you can use the when clause to specify conditions that must be met in order for a task to be executed. You can use the and and or operators to define multiple conditions in a when clause.

Here is an example of how to define multiple when conditions in Ansible:

- name: My task
  shell: do something
  when:
    - variable1 is defined
    - variable2 == "value"
    - variable3 is not defined or variable3 == "other value"

In this example, the task will be executed only if variable1 is defined, variable2 is equal to "value", and either variable3 is not defined or variable3 is equal to "other value".

You can also use the and and or operators to combine conditions using parentheses:

- name: My task
  shell: do something
  when:
    - (variable1 == "value1" and variable2 is defined) or variable3 == "value3"

In this example, the task will be executed if either variable1 is equal to "value1" and variable2 is defined, or variable3 is equal to "value3".

Created Time:2017-10-28 21:39:01  Author:lautturi