It is unnecessary to use '{{' in conditionals...

Hello,
Still the warning “It is unnecessary to use ‘{{’ in conditionals”.
I have read old posts on this subject and the ticket
https://github.com/ansible/ansible/issues/4582
I am using latest devel release.
But I don’t understand why I get this message and if I am using the good way of doing what I want.
I have to make a lot of tasks under certain conditions.
In order to simplify, I do something like this;

  • shell: something
    register st
  • set_fact: flag=0
  • set_fact: flag=1
    when: cur_sw.stdout == "something’
  • shell: something else
    when: flag==1

So, I use a fact as a boolean flag to skip or not following tasks.
If flag==1, I execute the following tasks and then update again flag according to the result.
And if still 1, I execute some other tasks.

My problem is that when I do:
when: flag==1
it does not work: it always skip the task.
In order to have it to work, I have to do:
when: “{{flag}}==1”
But when I do that, I have the warning:
It is unnecessary to use ‘{{’ in conditionals, leave variables in loop expressions bare.
I can avoid the warning by doing:
when: “1=={{flag}}”
but it is only a poor trick!

Please, could you explain me why
when: flag==1
does not work?

Thanks for your help!

When you use set_fact in a single line, it doesn’t know what type you mean:

set_fact: flag=1

Thus you want either:

when: flag|int == 1

OR:

when: flag == ‘1’

otherwise, pass a dictionary to set_fact, and types will be as you would expect:

set_fact:
flag: 1

Many thanks, that works.
I understand better the type problem.