• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

Ansible – Validate Multiple conditions for a task

December 6, 2020 By Cloud_Devops 2 Comments

How to add multiple conditions for an ansible task? Here are some of the examples which will help you to pick the right one according to your need. Ansible is an open-source market-leading configuration management tool. It’s a complete automation tool that can be used from On-premise to cloud platforms. In some cases, ansible is used as a provisioning tool as well.

Here are some of the examples which will help you to add conditions while writing the playbook.

Simple condition:

The task will be executed, when the variable “VAR1” is equal to the “value1″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 == "value"

The task will be executed, when the variable “VAR1” is not equal to the “value1″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 != "value"

Multiple condition:

AND Operator:

The task will be executed, when the variable “VAR1” is equal to the “value1″ and “VAR2″ is equal to the “value2″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 == "value1" and VAR2 == "value2"

The task will be executed, when the variable “VAR1” is not equal to the “value1″ and “VAR2″ is equal to the “value2″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 != "value1" and VAR2 == "value2"

The task will be executed, when the variable “VAR1” is not equal to the “value1″ and “VAR2″ is not equal to the “value2″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 != "value1" and VAR2 != "value2"

OR Operator:

The task will be executed, when the variable “VAR1” is equal to the “value1″ or “VAR2″ is equal to the “value2″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 == "value1" or VAR2 == "value2"

The task will be executed, when the variable “VAR1” is not equal to the “value1″ or “VAR2″ is equal to the “value2″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 != "value1" or VAR2 == "value2"

The task will be executed, when the variable “VAR1” is not equal to the “value1″ or “VAR2″ is not equal to the “value2″.

- name: validate single variable
  shell: echo "Hello World"
  when: VAR1 != "value1" or VAR2 != "value2"

Multiple “AND” Condition & “OR”

The task will be executed, when the variable “VAR1” is equal to the “value1″ or “VAR2″ is not equal to the “value2″.

- name: validate mutliple variables
   shell: echo "Hello World"
   when: (VAR1 == "value1")  or (VAR2 == "value2" and VAR3 == "value3")

Did you know ?

Conditions can be folded into multiple lines by using “>” symbol

Example:

- name: validate multiple variables
  shell: echo "Hello World"
  when: >
       (VAR1 == "value1")  or 
       (VAR2 == "value2" and VAR3 == "value3")

Hope this article is informative to you.

Filed Under: Ansible engine, Ansible Tower, AWX Tagged With: Ansible, Ansible multiple condition checks, Ansible Tower, ansible tutorial, check multiple conditions for a task - ansible, Validate multiple conditions in ansible

Reader Interactions

Comments

  1. bammbr says

    September 29, 2022 at 2:54 am

    The “OR Operator” section is a bull shit. Check it.

    Reply
    • Lingeswaran R says

      October 7, 2022 at 12:35 pm

      Updated

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version