Ansible allow us to put conditional control over playbook execution using when operation.
By using when condition you can control flow of execution only when it fulfill the defined criteria. In this topic we'll cover more such scenarios where we can see how to use when conditional operation with different formats.
Scenario 1. When condition using 'OR' operator
When condition with OR operator would be very useful when you want to check any of the condition in between two value. Lets take a look at below example where I have used when condition with OR operator. In this example I am comparing variable value of serverload ie 8 with when condition using OR operator of two different value.
[root@siddhesh ~]# cat when.yml
- hosts: dbserver
vars:
serverload: 8
tasks:
- debug:
msg: "server load is reached to 8 "
when: serverload==8 or serverload==6
[root@siddhesh ~]#
Here : when: serverload==8 or serverload==6 ==> variable value of serverload with OR operator.
[root@siddhesh ~]# ansible-playbook when.yml
PLAY [dbserver] *****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]
TASK [debug] ********************************************************************************************************************************************************
ok: [node1.tecgeek.info] => {
"msg": "server load is reached to 8 "
}
PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info : ok=2 changed=0 unreachable=0 failed=0
[root@siddhesh ~]#
In above playbook output you can see that execution of task only executed when serverload value matches with 8. Playbook could have skip this execution If this value set to other then 8.
Scenario 2: When condition using 'AND' operator
When condition with AND operator would be very useful when you want to check all condition before executing playbook actions. Lets take a similar example by having two different variable to compare its value with AND operator. In this example, I want to execute playbook only when serverload & cpuload is matches with value 8 & 4.
[root@siddhesh ~]# cat when.yml
- hosts: dbserver
vars:
serverload: 8
cpuload: 3
tasks:
- debug:
msg: "server load is reached to 8 "
when: serverload==8 and cpuload==4
[root@siddhesh ~]#
Here :
when: serverload==8 and cpuload==4 ==> variable value of serverload & cpuload to match with AND operator.
[root@siddhesh ~]# ansible-playbook when.yml
PLAY [dbserver] *****************************************************************************************************************************************************
TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [node1.tecgeek.info]
TASK [debug] ********************************************************************************************************************************************************
skipping: [node1.tecgeek.info]
PLAY RECAP **********************************************************************************************************************************************************
node1.tecgeek.info : ok=1 changed=0 unreachable=0 failed=0
[root@siddhesh ~]#
As per condition defined under playbook, playbook execution got skipped as it failed to match cpuload & serverload.
In similar way OR and AND operator can be used using following different operators to compare variable value.
<, lt, <=, le, >, gt, >=, ge, ==, =, eq, !=, <>, ne (Source)
Comments