# how to use "when: variableA == variableB"

**URL:** <https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885>\
**Category:** Ansible Project\
**Created:** [June 27, 2018, 1:15pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885 "2018-06-27T13:15:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [June 27, 2018, 1:15pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/1 "2018-06-27T13:15:56Z")

</div>

Hello

I would to run a specific role when two variables are equal to each other:

- name: Conrol Application  
hosts: [host1.example.com](http://host1.example.com)  
remote\_user: user1  
become: no  
gather\_facts: no

tasks:

- include\_role:  
name: appview-currentApp  
when: “{{ variableA }}” == “{{ variableB }}”

However, when I run the above, I get the following error:

[WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: “{{ variableA }}” ==  
“{{ variableB }}”

Any assistance will be gladly appreciated!

Thanks in advance.  
JS

---

<div class="post-metadata">

**Author:** ![mhalano](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mhalano/32/1815_2.png) [@mhalano](https://forum.ansible.com/u/mhalano)\
**Post date:** [June 27, 2018, 1:38pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/2 "2018-06-27T13:38:45Z")

</div>

Just use:  
when: variableA == variableB

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [June 27, 2018, 1:54pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/3 "2018-06-27T13:54:04Z")

</div>

Thanks for the swift response Marcos!

I already tried to use ‘when: variableA == variableB’ but then it literally evaluates variableA and variableB. Not the outputs of variableA and variableB. And that results in:

skipping: [localhost] =\> {  
“changed”: false,  
“skip\_reason”: “Conditional result was False”  
}

Sometimes variableA could be abc and in that case variableB should also be abc  
Sometimes variableA could be 123 and in that case variableB should also be 123

So the when needs to evaluate the contents of each variable, then match them.

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [June 27, 2018, 2:01pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/4 "2018-06-27T14:01:34Z")

</div>

It works for me.

Is it possible that the variables could be different types? Maybe it thinks you’re comparing a string to an int, etc.

For instance, it works for me if I set:

variableA: 1  
variableB: 1

But not if I set:

variableA: 1  
variableB: “1”

–Steve

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [June 27, 2018, 2:14pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/5 "2018-06-27T14:14:15Z")

</div>

Ah strange.

One of the Variables (variableB) gets passed through an --extra-vars in the CLI.

variablA gets picked up from an xmlFile.

So the idea, is that the variableA in the xmlFile should always match the output of variableB that’s passed through the CLI.

Regards

JS

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [June 27, 2018, 2:43pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/6 "2018-06-27T14:43:52Z")

</div>

Here is a test playbook, save it somewhere as test.yaml:

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [June 27, 2018, 2:47pm UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/7 "2018-06-27T14:47:47Z")

</div>

Maybe try casting the variables to a string in your when conditional:

when: variableA|string == variableB|string

---

<div class="post-metadata">

**Author:** ![JS11](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@JS11](https://forum.ansible.com/u/JS11)\
**Post date:** [June 28, 2018, 8:39am UTC](https://forum.ansible.com/t/how-to-use-when-variablea-variableb/27885/8 "2018-06-28T08:39:36Z")

</div>

Hi Karl

That was a fantastic test to run! Many thanks for sending that across.

I first used your test yaml file to run it with the variables you had set in place. I then replaced your variables with my variables, and found that one of my variables was outputting a trailing back-slash \ at the end of the string - which is why the Conditional was resulting in a False.

All sorted now.

Once again, Thank you so much - highly appreciated!

Regards  
JS
