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

349 views
Skip to first unread message

Olivier Javaux

unread,
Aug 31, 2014, 2:11:29 AM8/31/14
to ansible...@googlegroups.com
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!




Michael DeHaan

unread,
Sep 1, 2014, 2:05:06 PM9/1/14
to ansible...@googlegroups.com
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





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/55b02402-0d36-44b7-9ec6-2c2554a3e44c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Olivier Javaux

unread,
Sep 2, 2014, 4:47:22 AM9/2/14
to ansible...@googlegroups.com
Many thanks, that works.
I understand better the type problem.
Reply all
Reply to author
Forward
0 new messages