# How can we write if else logic using shell or command.. I can not use when clauseas it does not give me expected result

**URL:** <https://forum.ansible.com/t/how-can-we-write-if-else-logic-using-shell-or-command-i-can-not-use-when-clauseas-it-does-not-give-me-expected-result/24191>\
**Category:** Ansible Project\
**Created:** [April 11, 2017, 2:20pm UTC](https://forum.ansible.com/t/how-can-we-write-if-else-logic-using-shell-or-command-i-can-not-use-when-clauseas-it-does-not-give-me-expected-result/24191 "2017-04-11T14:20:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [April 11, 2017, 2:20pm UTC](https://forum.ansible.com/t/how-can-we-write-if-else-logic-using-shell-or-command-i-can-not-use-when-clauseas-it-does-not-give-me-expected-result/24191/1 "2017-04-11T14:20:56Z")

</div>

How can we write if else logic using shell or command… I can not use when clauseas it does not give me expected result

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 11, 2017, 4:23pm UTC](https://forum.ansible.com/t/how-can-we-write-if-else-logic-using-shell-or-command-i-can-not-use-when-clauseas-it-does-not-give-me-expected-result/24191/2 "2017-04-11T16:23:09Z")

</div>

What is it that you are trying to do? It might be possible to do another way.

Sometimes I use two set\_fact tasks with mutually exclusive when conditions.

As long as you remember that you don't need "{{ }}" on your when conditions and the conditions evaluate to a boolean, when conditions can be useful.

Jon

---

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [April 11, 2017, 5:33pm UTC](https://forum.ansible.com/t/how-can-we-write-if-else-logic-using-shell-or-command-i-can-not-use-when-clauseas-it-does-not-give-me-expected-result/24191/3 "2017-04-11T17:33:31Z")

</div>

Hi,

Can you please once review my question -Ansible -Need to reset the value of variable before command.

I need assistance for that question, and i am trying different ways for it.

---

<div class="post-metadata">

**Author:** ![nishant\_bawane](https://avatars.discourse-cdn.com/v4/letter/n/919ad9/32.png) [@nishant\_bawane](https://forum.ansible.com/u/nishant_bawane)\
**Post date:** [April 11, 2017, 5:46pm UTC](https://forum.ansible.com/t/how-can-we-write-if-else-logic-using-shell-or-command-i-can-not-use-when-clauseas-it-does-not-give-me-expected-result/24191/4 "2017-04-11T17:46:29Z")

</div>

This is below question in which i need to reset the value of one of the variable -

Query: I need to fetch the latest java version from host servers and display the same on web page, using apache server which is installed on my local server(local server means server with ansible installed).

I am able to fetch the value, but need to reset the value of one of the variable to blank , below are the details.

**I need to make it run time activity like —**

**when i have Both Hosts : zlp12036 and zlp12037, it must show the java version for both on my local server which has apache installed**  
**when i have Only one Host i.e.zlp12036, it must show the java version for zlp12036 only**  **on my local server which has apache installed.**

Host File Details -

[myserver]

zlp12036 ansible\_ssh\_host=[zlp12036.vci.att.com](http://zlp12036.vci.att.com) ansible\_ssh\_user=abc  
#zlp12037 ansible\_ssh\_host=[zlp12037.vci.att.com](http://zlp12037.vci.att.com) ansible\_ssh\_user=abc

Playbook:
