Ansible Conditions

In an ansible playbook, we sometimes need to execute different tasks depending on the value of a variable or the value of the return variable of a previous task. You may want to perform a certain set of tasks if the value of the variable is less than a value and a different set of tasks if the value of the variable is greater. You can achieve this with Ansible conditions.

In this guide, we will demonstrate how to use various when conditional statements.

Using 'when' conditional statement

Ansible uses Jinja2 tests and filters in conditionals.

We are going to use the 'when' conditional statement to create a file in the playbook as shown:

- name: this is our first play.
  hosts: webserver1
  vars:
    age: 17
  tasks:
    - name: "creating file using variable"
      command: touch /tmp/18.txt
      when: age == 18

The value of var age is 17 which is not satisfying the when condition age =18, hence the task 'creating the file using variable' will not be executed.

When you run the playbook, you'll get the output below- ansible-condition-output

No file is created on webserver1 - ansible-condition-output

Now, let's modify the playbook and change the value of var age = 18 to satisfy the when condition and run the playbook again.

- name: this is our first play.
  hosts: webserver1
  vars:
    age: 18
  tasks:
    - name: "creating file using variable"
      command: touch /tmp/18.txt
      when: age == 18

Output after executing this playbook - ansible-condition-output

Verify the same on webserver1 - ansible-condition-output

Using multiple 'when' conditional statement

Consider below playbook2.yml -

- name: this is our first play.
  hosts: webserver1
  vars:
    age: 19
  tasks:
    - name: "creating file using variable"
      command: touch /tmp/18.txt
      when: age == 18
    - name: "creating file using variable"
      command: touch /tmp/grater_then_18.txt
      when: age > 18

Here we have 2 conditions. First condition is not true as the age == 18 and second is true because age > 18.

When you run the playbook, you'll get the output below- ansible-condition-output

Verify the same on webserver1

ansible-condition-output

Using when with the logical AND operator

Now suppose we have three 'when' conditional statement along with the logical AND operator in the playbook as shown:

- name: this is our first play.
  hosts: webserver1
  vars:
    age: 15
  tasks:
    - name: "creating file using variable"
      command: touch /tmp/18.txt
      when: age == 18
    - name: "creating file using variable"
      command: touch /tmp/grater_then_18.txt
      when: age > 18
    - name: "creating file using variable"
      command: touch /tmp/less_then_10.txt
      when: age < 10
    - name: "creating file using variable"
      command: touch /tmp/between_10_and_18.txt
      when: age > 10 and age < 18

Here only the fourth condition is getting satisfied.

Checking the same on Webserver1

ansible-condition-output

Using when with the logical OR Operator

With OR logical operators, the play is executed when either or all of the conditions are satisfied.

Let's consider another playbook:

- name: this is our first play.
  hosts: webserver1
  vars:
    age: 11
  tasks:
    - name: "creating file using variable"
      command: touch /tmp/18.txt
      when: age == 18
    - name: "creating file using variable"
      command: touch /tmp/grater_then_18.txt
      when: age > 18
    - name: "creating file using variable"
      command: touch /tmp/less_then_10.txt
      when: age < 10
    - name: "creating file using variable"
      command: touch /tmp/between_10_and_18.txt
      when: age > 10 and age < 18
    - name: "creating file using variable"
      command: touch /tmp/10_or_11.txt
      when: age == 10 or age == 11

The above playbook satisfies the last two conditions. Verifying the same

Conditional statements are very useful in a multi-OS setup where we need to do variations in server configuration.