Why can when: run_my_task behave wrongly if run_my_task was set as "false" in quotes, and how do you guard against it?
Quoted, "false" is a non-empty string, not a boolean; older ansible-core treats it as true and runs the task, newer releases reject it. when: run_my_task | bool converts it properly.
* Quoting turns the word false into a string; the bool filter turns it back. *
YAML decides the type when the file is parsed:
run_my_task: falseis the boolean false, so the task is skipped.run_my_task: "false"is the string"false", five characters long.
From ansible-core 2.12 up to 2.18, a non-empty string in when counts as true, so the quoted version runs the task, which is almost certainly not what the author meant. Since ansible-core 2.19, a conditional whose result is not a boolean is an error by default ("Conditionals must have a boolean result"), so the mistake now stops the play instead of passing silently.
The guard is the same in both cases. The bool filter turns known truthy and falsy strings ("false", "no", "0", "yes", ...) into real booleans and passes real booleans through unchanged:
when: run_my_task | bool
This matters most for values that arrive as strings, such as extra variables given on the command line with -e.
Go deeper:
Conditionals based on variables (official docs) — why string variables need
| bool.ansible.builtin.bool filter (official docs) — which strings it converts.
Ansible-core 2.19 porting guide: broken conditionals (official docs) — the change that makes non-boolean conditionals an error.