# WHEN CONDITION

**URL:** <https://forum.ansible.com/t/when-condition/23591>\
**Category:** Ansible Project\
**Created:** [January 30, 2017, 10:24am UTC](https://forum.ansible.com/t/when-condition/23591 "2017-01-30T10:24:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [January 30, 2017, 10:24am UTC](https://forum.ansible.com/t/when-condition/23591/1 "2017-01-30T10:24:04Z")

</div>

I want to use the following condition in ansible playbook.

Scenario is as follows.

If arg1 \> 5  
follow some steps  
else  
endif;

IF ELSE is not possible in ansible. However, how can i achive with the WHEN condition.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 30, 2017, 2:29pm UTC](https://forum.ansible.com/t/when-condition/23591/2 "2017-01-30T14:29:20Z")

</div>

Just add a when to the tasks you want in the ‘if’  
when: arg1 \> 5

for the ‘else’ just use the opposite condition.

when: arg1 \<=5

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [January 30, 2017, 6:02pm UTC](https://forum.ansible.com/t/when-condition/23591/3 "2017-01-30T18:02:08Z")

</div>

I did create the small task. However, for defining variable (num, numami) in the same task is not working.

e.g

tasks:

- name: Condition statement  
shell: date\_string=`date +%d%b%G`; num=3; numami=5;  
when: $numami \> $num  
echo Hi;

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 30, 2017, 6:23pm UTC](https://forum.ansible.com/t/when-condition/23591/4 "2017-01-30T18:23:16Z")

</div>

These are not valid variables for Ansible (not since 1.6). Also, setting them in a shell will not make them available.

when: $numami \> $num

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 1, 2017, 7:16pm UTC](https://forum.ansible.com/t/when-condition/23591/5 "2017-02-01T19:16:30Z")

</div>

Where i can set this variable so that i can compare it in where condition.

Any small test scenario will help me to understand.

---

<div class="post-metadata">

**Author:** ![Rodrigo\_B\_Brasil](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rodrigo\_B\_Brasil](https://forum.ansible.com/u/Rodrigo_B_Brasil)\
**Post date:** [February 1, 2017, 7:46pm UTC](https://forum.ansible.com/t/when-condition/23591/6 "2017-02-01T19:46:18Z")

</div>

This should help:

[http://docs.ansible.com/ansible/playbooks\_variables.html](http://docs.ansible.com/ansible/playbooks_variables.html)

[http://docs.ansible.com/ansible/set\_fact\_module.html](http://docs.ansible.com/ansible/set_fact_module.html)

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 6, 2017, 7:00pm UTC](https://forum.ansible.com/t/when-condition/23591/7 "2017-02-06T19:00:36Z")

</div>

Tried below one. However, when condition is not correctly evaluating.

Even following condition true its executing other commands mentioned in other when condition.  
current\_image\_state \<= desire\_image\_state

tasks:

- name: Get the state of the Test version  
shell: alert1=`cat /tmp/test.log | grep -e "pending" -e "failed" | wc -l`;  
register: image\_state  
tags: image

- name: Set the output of the Test state  
set\_fact: current\_image\_state=image\_state.stdout  
tags: Test1

- name: Define our static value for the desired Test state  
set\_fact: desire\_image\_state=0  
tags: Test2

- name: Perform TASK1  
command: sh /tmp/test.sh  
when: current\_image\_state \> desire\_image\_state  
tags: final

- name: Message if couldnot able to perform TASK1  
debug: msg=“Need to check the status of Pending image”  
when: current\_image\_state \<= desire\_image\_state  
tags: message

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [February 7, 2017, 12:04pm UTC](https://forum.ansible.com/t/when-condition/23591/8 "2017-02-07T12:04:30Z")

</div>

> Tried below one. However, when condition is not correctly evaluating.

Please show the exact error message. Or the exact output, maybe with -v.

> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;- name: Get the state of the Test version  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: alert1=`cat /tmp/test.log | grep -e "pending" -e "failed" | wc  
> -l`;  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: image\_state  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: image

Add a debug task here (with correct indentation):  
- debug: var="image\_state"

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: current\_image\_state=image\_state.stdout

I guess here is your error. You set the variable current\_image\_state  
to a text string called "image\_state.stdout". Add double curly braces  
and quotes:

set\_fact: current\_image\_state="{{ image\_state.stdout }}"

> &nbsp;&nbsp;&nbsp;- name: Define our static value for the desired Test state  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact: desire\_image\_state=0

I am not sure if thus sets desire\_image\_state to an integer or a string.

Johannes

---

<div class="post-metadata">

**Author:** ![DHAVAL\_JAISWAL](https://avatars.discourse-cdn.com/v4/letter/d/34f0e0/32.png) [@DHAVAL\_JAISWAL](https://forum.ansible.com/u/DHAVAL_JAISWAL)\
**Post date:** [February 7, 2017, 1:16pm UTC](https://forum.ansible.com/t/when-condition/23591/9 "2017-02-07T13:16:27Z")

</div>

Thanks !!

It works, post adding set\_fact: current\_image\_state=“{{ image\_state.stdout }}”.
