# Ansible handler with when condition

**URL:** <https://forum.ansible.com/t/ansible-handler-with-when-condition/2366>\
**Category:** Get Help\
**Created:** [November 14, 2023, 2:56pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366 "2023-11-14T14:56:01Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![laurent-indermuehle](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/laurent-indermuehle/32/289_2.png) [@laurent-indermuehle](https://forum.ansible.com/u/laurent-indermuehle)\
**Post date:** [November 14, 2023, 8:00pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/5 "2023-11-14T20:00:55Z")

</div>

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:

```yml
when: use_handler is true

```

is:

```yml
when: use_handler | bool is true

```

Or even better (and supported since Ansible 2.10):

```yml
when: use_handler is truthy(convert_bool=True)

```

From the documentation: [Tests — Ansible Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tests.html#testing-truthiness)

---

_[View the full topic](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366)._
