# odd problem with conditionals

**URL:** <https://forum.ansible.com/t/odd-problem-with-conditionals/19822>\
**Category:** Ansible Developer\
**Created:** [November 20, 2015, 9:20pm UTC](https://forum.ansible.com/t/odd-problem-with-conditionals/19822 "2015-11-20T21:20:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nueces\_dots](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@nueces\_dots](https://forum.ansible.com/u/nueces_dots)\
**Post date:** [November 20, 2015, 9:20pm UTC](https://forum.ansible.com/t/odd-problem-with-conditionals/19822/1 "2015-11-20T21:20:04Z")

</div>

Hí all,

I have a really odd problem with a conditional evaluation in a set\_fact, this is a excerpt in where the result of the evaluation depend on the order of the variables, see it.

tasks.yml

---

<div class="post-metadata">

**Author:** ![Tim1](https://avatars.discourse-cdn.com/v4/letter/t/b782af/32.png) [@Tim1](https://forum.ansible.com/u/Tim1)\
**Post date:** [December 15, 2015, 5:41am UTC](https://forum.ansible.com/t/odd-problem-with-conditionals/19822/2 "2015-12-15T05:41:16Z")

</div>

This is happening because the values that are being set inside of the ansible\_facts in your tasks are strings, not booleans.

ex.  
{“ansible\_facts”: {“updated\_sources”: “True”}}

The above is the _string_ “True”. Not the boolean True. Likewise,

{“ansible\_facts”: {“configuration\_changed”: “False”}}

Is the _string_ “False”, which is, of course, equivalent to boolean True. ([http://www.thomas-cokelaer.info/tutorials/python/boolean.html](http://www.thomas-cokelaer.info/tutorials/python/boolean.html))

So when your conditionals are evaluated in the set\_fact, you’re basically comparing only boolean True values. The reason that you get “False” there is because the string “False” is equivalent to boolean True. So the value you’re seeing here

{“ansible\_facts”: {“appclone\_pm\_must\_restart1”: “False”, “appclone\_pm\_must\_restart2”: “True”}}

Is actually the string, not the boolean.

You can see this behavior with this simple python script.

“”"  
#!/usr/bin/env python

ok1 = “False”  
ok2 = “True”  
ok3 = True  
ok4 = False  
ok5 = “False2”

print (ok1 or ok2)  
print (ok2 or ok1)  
print (ok4 or ok3)  
print (ok3 or ok4)  
print (ok5 or ok2)  
“”"

The output of which looks like this

$ python ok.py  
False  
True  
True  
True  
False2

In your case, you’re seeing the equivalent of my “False2” above. If you want to fix your playbook, you have 2 options.

1. Return booleans in your first 2 tasks instead of strings
2. Cast the values in your set\_fact to bool

For #2, it would look like this.

…

tasks:

- set\_fact:  
appclone\_pm\_must\_restart1: “{{ configuration\_changed|bool or updated\_sources|bool }}”  
appclone\_pm\_must\_restart2: “{{ updated\_sources|bool or configuration\_changed|bool }}”

…

Hope that helps.  
-tim
