29.6 C
Thursday, September 23, 2021

How to define multiple when conditions in Ansible

Must read

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
Est. reading time 2 minutes


In this following example, I will add multiple when conditions for an Ansible task in our playbooks. The following criteria explain the correct syntax for logical “OR” and “AND” conditions.
How 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:

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

We can also use ansible facts along with when condition:

  - 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:

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

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
          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.


Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

- Advertisement -

Latest article