how to use "when: variableA == variableB"

Hello

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

  • name: Conrol Application
    hosts: 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

Just use:
when: variableA == variableB

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.

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

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

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

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

when: variableA|string == variableB|string

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