odd problem with conditionals

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

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)

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