Ansible “when variable == true” not behaving as expected

You need to convert the variable to a boolean:

force_install|bool == true

I don’t claim I understand the logic behind it. In python any non-empty string should be truthy. But when directly used in a condition it evaluates to false.

The bool filter then again interprets the strings 'yes', 'on', '1', 'true' (case-insensitive) and 1 as true (see source). Any other string is false.

You might want to also set a default value in case force_install is not defined, since it would result in an undefined variable error:

force_install|default(false)|bool == true

Leave a Comment