# Ansible not evaluating parameters correctly

**URL:** <https://forum.ansible.com/t/ansible-not-evaluating-parameters-correctly/18252>\
**Category:** Ansible Project\
**Created:** [May 18, 2015, 3:15pm UTC](https://forum.ansible.com/t/ansible-not-evaluating-parameters-correctly/18252 "2015-05-18T15:15:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petar\_Petkov](https://avatars.discourse-cdn.com/v4/letter/p/90ced4/32.png) [@Petar\_Petkov](https://forum.ansible.com/u/Petar_Petkov)\
**Post date:** [May 18, 2015, 3:15pm UTC](https://forum.ansible.com/t/ansible-not-evaluating-parameters-correctly/18252/1 "2015-05-18T15:15:47Z")

</div>

Lets suppose we have the following code sample:

`

```auto
- 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?

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 18, 2015, 4:11pm UTC](https://forum.ansible.com/t/ansible-not-evaluating-parameters-correctly/18252/2 "2015-05-18T16:11:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ivaylo\_Bratoev](https://avatars.discourse-cdn.com/v4/letter/i/f9ae1b/32.png) [@Ivaylo\_Bratoev](https://forum.ansible.com/u/Ivaylo_Bratoev)\
**Post date:** [May 18, 2015, 6:53pm UTC](https://forum.ansible.com/t/ansible-not-evaluating-parameters-correctly/18252/3 "2015-05-18T18:53:54Z")

</div>

Seems related to:  
[https://github.com/ansible/ansible/issues/5094](https://github.com/ansible/ansible/issues/5094)  
[https://github.com/ansible/ansible/issues/10460](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/b-LtXWtsPnE/yf8JVnGEUf4J)  
[https://groups.google.com/forum/#!searchin/ansible-project/boolean/ansible-project/PcVKMT0Mw9Y/gVqt8YdCpVUJ](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?
