Ansible handler with when condition

I want to add (because I learned that the hard way) that you should always transform your “bool” variable with the bool filter in the playbook. If one day you decide to use AWX/AAP, your variable will always be a string even if you thought you send a bool.

So the correct way to write your line:

when: use_handler is true

is:

when: use_handler | bool is true

Or even better (and supported since Ansible 2.10):

when: use_handler is truthy(convert_bool=True)

From the documentation: Tests — Ansible Documentation

4 Likes