# When conditional can be used with variables??

**URL:** <https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899>\
**Category:** Ansible Project\
**Created:** [March 17, 2016, 3:23am UTC](https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899 "2016-03-17T03:23:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NEW\_HOMIE](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@NEW\_HOMIE](https://forum.ansible.com/u/NEW_HOMIE)\
**Post date:** [March 17, 2016, 3:23am UTC](https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899/1 "2016-03-17T03:23:05Z")

</div>

Hello there~  
Is this possible using when conditional with variables?

For instance, I use two kind of was (Jboss, Weblogic)

I create for checking process id some yml files like under line

**check.yml**

- shell: ps -ef | grep ‘{{ nodename }}’ | grep XX | awk {‘print $2’}

register: pid  
ignore\_errors: True  
when: {{ was }} == jboss

- shell: /usr/ucb/ps -auxwww | grep ‘{{ nodename }}’ | grep XX | awk {‘print $2’}  
register: pid  
ignore\_errors: True  
when: {{ was }} == weblogic

and then, put this yml file in main.yml

**main.yml**

- include: check.yml  
tags: check

now I would execute this command

# ansible-playbook playbook -i inventory -t check -e component=somecomponent -e nodename=somenodename -e was=jboss

however It gives me under error message?  
{“failed”: true, “reason”: “ERROR! Syntax Error while loading YAML.  
The error appears to have been in ‘check.yml’: line 5, column 19, but maybe elsewhere in the file depending on the exact syntax problem.  
The offending line appears to be: ignore\_errors: True\n when: {{ was }} == jboss^ here  
We could be wrong, but this one looks like it might be an issue with missing quotes.  
Always quote template expression brackets when they start a value. For instance: with\_items: - {{ foo }} Should be written as: with\_items: - "{{ foo }}"”}

What’s problem in my check.yml??

---

<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:** [March 17, 2016, 10:08pm UTC](https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899/2 "2016-03-17T22:08:44Z")

</div>

you are writing your conditionals wrong, vars don’t need {{}} and strings need quoting:

when: was == ‘jboss’

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 17, 2016, 11:53pm UTC](https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899/3 "2016-03-17T23:53:16Z")

</div>

I might be misinformed but I believe that syntax will be deprecated soon and the best way to write that conditional is:  
when: ‘{{ was }}’ == ‘jboss’

---

<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:** [March 18, 2016, 12:14am UTC](https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899/4 "2016-03-18T00:14:12Z")

</div>

you are misinformed

---

<div class="post-metadata">

**Author:** ![Arthur\_Reyes](https://avatars.discourse-cdn.com/v4/letter/a/90ced4/32.png) [@Arthur\_Reyes](https://forum.ansible.com/u/Arthur_Reyes)\
**Post date:** [March 18, 2016, 12:21am UTC](https://forum.ansible.com/t/when-conditional-can-be-used-with-variables/20899/5 "2016-03-18T00:21:59Z")

</div>

Rereading the porting to 2.0 guide, there are a number of instances where bare variables do produce warnings of future deprecation, but when conditions are not mentioned there.

Apologies for any confusion I may have caused.
