Seems related to:
https://github.com/ansible/ansible/issues/5094 
https://github.com/ansible/ansible/issues/10460
https://groups.google.com/forum/#!searchin/ansible-project/boolean/ansible-project/b-LtXWtsPnE/yf8JVnGEUf4J
https://groups.google.com/forum/#!searchin/ansible-project/boolean/ansible-project/PcVKMT0Mw9Y/gVqt8YdCpVUJ

It seems this is a source of a lot of confusion. Looking at it - it should 
be a best practice to use the *bool* filter whenever you use *when*. The 
documentation doesn't do that and this leads to confusion.

I am interested on the opinions of others - can this be fixed in Ansible to 
work as expected or should casting to *bool* should be a best practice in 
*when*-s? 

On Monday, May 18, 2015 at 7:11:50 PM UTC+3, Matt Martz wrote:
>
> This is mostly due to how ansible casts values when dereferencing.
>
> It is always safest to use the |bool filter when evaluating a variable 
> that you think should be a boolean.
>
> Such as:
>
> when: not bar|bool
>
> Otherwise, in your example it is evaluating the string value "no" instead 
> of a falsely YAML no value.  And a non-zero length string evaluates as 
> truthy.
>
> On Mon, May 18, 2015 at 10:15 AM, Petar Petkov <[email protected] 
> <javascript:>> wrote:
>
>> Lets suppose we have the following code sample:
>>
>> - name: Bug report
>>   hosts: localhost
>>   gather_facts: no
>>   connection: local
>>   vars:
>>     foo: no
>>     bar: "{{ foo }}"
>>   tasks:
>>     - debug: msg='This should not be skipped!'
>>       when: not bar
>>
>>
>>
>> Seeing the code one would expect that the message "This should not be 
>> skipped" is displayed when the code is executed. In reality, however, the 
>> code gets skipped because the bar parameter is not correctly resolved to 
>> its value *no. *Rewriting the when clause to look like this:
>> when: not {{ bar }}
>>
>> seems to fix the problem. 
>> Also it seems that this problem is only caused when the *not* operator 
>> is used, because if I use the following code sample:
>>
>> - name: Bug report
>>   hosts: localhost
>>   gather_facts: no
>>   connection: local
>>   vars:
>>     foo: yes
>>     bar: "{{ foo }}"
>>   tasks:
>>     - debug: msg='This should not be skipped!'
>>       when: bar
>>
>>
>> everything seems to work as expected.
>>
>> So is this a bug (perhaps a known issue) or am I doing something wrong?
>>
>> -- 
>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected] 
>> <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/ansible-project/75974d5e-0fc8-4d37-b088-9950ff0abe5c%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/ansible-project/75974d5e-0fc8-4d37-b088-9950ff0abe5c%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Matt Martz
> @sivel
> sivel.net
>  

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/1143d2de-0d28-4c61-8884-f15396cf4cb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to