How to define multiple when conditions in Ansible nixCraft

Sometimes we need to evaluate multiple when conditions in Ansible playbooks. Let us see how to define multiple when conditions in Ansible for a single task.

This page explains how to define multiple when conditions in Ansible. The examples further tell how to do a logical “OR” or “AND” using Ansible IT automation tool on your macOS, Linux, or Unix desktop.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Ansible
How to define multiple when conditions in AnsibleHow to define multiple when conditions in Ansible

Simple “when” condition example

We use the when statement for a single task. The syntax is as follows in your playbook:

tasks: - name: Let us update box when OS is Debian apt: update_cache: yes cache_valid_time: 3600 upgrade: dist when: ansible_distribution == "Debian"

We can also use ansible facts along with when condition:

tasks: - name: Reboot Ubuntu systems ansible.builtin.command: /sbin/shutdown -r now when: ansible_facts['os_family'] == "Ubuntu"

Checking for multiple conditions using “when” in Ansible

We can define multiple conditions such as reboot the box if OS is Debian or Ubuntu but skip all other operating systems as follows using a logical OR with when clause as follows:

tasks: - name: Reboot Debian or Ubuntu Linux box ansible.builtin.command: /sbin/shutdown -r now when: (ansible_distribution == "Debian" ) or (ansible_distribution == "Ubuntu")

Logic operators in Jinja2

  1. and : Boolean and
  2. or : Boolean or
  3. not : Boolean negation

For example:

# run when os is debian and nginx version 1.8.0 not found 
- hosts: webservers

- shell: /sbin/nginx -v 2>&1 register: currentNginxVersion
 roles: - role: debian_config when: (ansible_facts['os_family'] == 'Debian') and ('"nginx/1.8.0" not in currentNginxVersion.stdout')

Defining multiple when conditions in Ansible

Let us see an example of a logical AND. I want to reboot Debian or Ubuntu Linux system after kernel update, and the inventory hostname must be aws-proxy-server. If both conditions are true, then issue the reboot command using the Ansible reboot module. Otherwise, skip the reboot option.

Step 1 – Create a new “reboot_file” variable

 - name: Check if a reboot is needed on all Ubuntu/Debian based servers register: reboot_file stat: path=/var/run/reboot-required get_md5=no

Step 2 – Create the Ansible multiple when condition

 - name: Reboot the box if kernel updated and hostname == aws-proxy-server reboot: msg: "Reboot initiated by Ansible for kernel updates" connect_timeout: 5 reboot_timeout: 300 pre_reboot_delay: 0 post_reboot_delay: 30 test_command: uptime when: (reboot_file.stat.exists) and (inventory_hostname == 'aws-proxy-server')

Step 3 – Test it

Run it as follows:

ansible-playbook -i hosts aws.yaml
ansible-playbook -i hosts --ask-vault-pass --extra-vars '@private.data.yml' aws.yaml

Summing up

I hope these simple examples will help you with logical “OR” or “AND” when conditions under the Ansible. See the Ansible documentation for more information.

ADVERTISEMENT

Posted by Contributor