Technology

How to define multiple when conditions in Ansible

There is no greater hope in human life than survival. At the same time, it is important to live well. With that in mind, every content on the website is written so that a person can get all the information from here to start his life to make beautifully.

According to that, Technology is one of the topics. It is also a part of life. Read carefully Details of Technology related article

How to define multiple when conditions in Ansible

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

Syntax

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:

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:

 

Patreon supporters only guides 🤓

    • No ads and tracking

 

 

    • Join my Patreon to support independent content creators and start reading latest guides:

 

Join Patreon
# 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
 

 

Did you like this article?
Share it on any of the following social media channels below to give us your vote. Your feedback helps us improve.

Other related Technologies ideas you might enjoy

Related Articles

Leave a Reply

Your email address will not be published.