Ansible 'When' Conditionals - More about it ?

Ansible is a configuration management tool used for automating remote system management tasks like software deployment and installing updates, configuration management, orchestration, and many other IT tasks.

Here at LinuxAPT , we shall look into the "When" statement and how to use the "When" statement in different situations.


Examples of using the "When" statement

Here, we will use various "When" conditional statements in a setup with servers running different operating systems.

Let's say:

  • We have installed Ansible on your system(Ubuntu 20.04 in our case).
  • We know what Ansible is used for and how to set up a basic local testing environment.


In the Setup, we have an Ansible controller host with Ubuntu 20.04 system. There are two target nodes: one with the Ubuntu 20.04 system and another one with Debian Buster.

Suppose you are working on a certain Ansible project, and at a certain point, you want to skip a specific step for a specific host.

To understand it better, let's take an example, suppose you want to skip installing some sort of package on a host if the required version of the software is not available. This is where the "When" clause comes in handy.


1. A Simple Conditional With "when"

A basic conditional statement works with a single task. What you have to do is to create a task and then add a condition using the "when" statement. This "when" statement applies to a test or simply a condition for this task. 

An example is given below:

- name: Running shutdown command on CentOS-based systems
ansible.builtin.command: /sbin/shutdown -t now
when: ansible_facts['os_family'] == "CentOS"

When a task or playbook is executed, the tests are checked against all the hosts. If the test is passed for any host, Ansible will execute the task; otherwise, it will skip the execution. In the above example, the shutdown command will be executed for any system that matches the criteria of being a CentOS-based OS.


2. When using the "ansible_facts" With the "when" Conditional

A task can also be skipped based on facts. Facts are property of a host, such as IP of the host, type of operating system, etc. 

The following are some fact-based use cases:

  • Installing a package for a particular OS version.
  • Skipping firewall configuration on hosts with private IP addresses.
  • Shutting down system with specific OS family.


Multiple conditions for a "when" clause can also be merged. This uses logical operators like or not. 

An example is given below:

- name: Shut off Debian 10 and Debian 11 systems
ansible.builtin.command: /sbin/shutdown -t now
when: (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "10") or
(ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "11")

As previously explained, Ansible performs conditional evaluations on the targets(hosts) prior to running a task. In the task above, If the condition, i.e., the system is Debian 10 or Debian 11, is found to be true, Ansible will execute the requested shutdown task. If the condition is unmet, Ansible will simply skip this task.

If multiple tasks are required to be true simultaneously(a logical "and" condition), we can list them as:

- name: Shut off Ubuntu 17.04 systems
ansible.builtin.command: /sbin/shutdown -t now
- ansible_facts['distribution'] == "Ubuntu"
- ansible_facts['distribution_major_version'] == "17.04"



3. When Using the "registered variables" With "when" Conditional

In Ansible, registered variables can also be used with conditionals. These variables are obtained from the output of a task in a play and can be used as a base for further tasks.

An Example is given below:

- hosts: web_servers
- name: Running a shell command and registering its outcome as a variable using the 'register' keyword. /usr/bin/pwd
register: var_object
- name: Running a shell command based on the result of the last task /usr/bin/ls
when: var_object.stdout = =”ubuntu”



Examples of how the when clause works in Ansible ?

1. Basic Usage

Here, Ansible will check if a specific user exists on the remote host. If the user’s (myuser) directory is found, it will create a file(myfile.txt) inside this directory. 

First, create a playbook (my-playbook.yml) with the below content:

- hosts: all
gather_facts: no
become: true
- name: Check if the user is present or not.
path: /home/vagrant
register: myuser
- name: If the directory is present, then create a file(myfile.txt)
path: /home/vagrant/myfile.txt
state: touch
when: myuser.stat.exists


2. When Usage-based on ansible_facts

Here, Ansible will gather facts about the OS family and shut off all the Ubuntu-based remote systems using the when statement:

- hosts: all
gather_facts: yes
become: true
- name: shutdown Debian-based servers
ansible.builtin.command: /sbin/shutdown -t now
when: ansible_facts['os_family']=="Debian"


[Need help in fixing Ansible issues ? We can help you. ]

This article covers how to use the "when" clause in Ansible. In fact, In Ansible, you can define conditions that will be evaluated before a task is executed. When a condition is not met, the task is then skipped. This is done with the when keyword, which accepts expressions that are typically based on a variable or a fact.

Related Posts