# Using "when:" on handlers

**URL:** <https://forum.ansible.com/t/using-when-on-handlers/1952>\
**Category:** Get Help\
**Tags:** playbook, ansible-core\
**Created:** [October 25, 2023, 1:22pm UTC](https://forum.ansible.com/t/using-when-on-handlers/1952 "2023-10-25T13:22:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [October 25, 2023, 1:22pm UTC](https://forum.ansible.com/t/using-when-on-handlers/1952/1 "2023-10-25T13:22:15Z")

</div>

We’ve had a snippet, a comment really, floating around our shop for a long time, to wit:

```yaml
# This task file is included by the "soft" handler b/c
# handlers can't deal with `when:` etc.

```

I suspect it was born of empirical evidence from a `when:` expression that used an undefined variable and we were just learning Ansible. My guess is that, instead of the “define all variables or provide defaults” lesson, somebody leapt to the wrong conclusion that “handlers can’t deal with `when:`”.

The only restriction I see in the docs on handlers is a prohibition on their use of `import_role` or `include_role`. We just had an embarrassing oops in production because of this comment, and I’d like to settle this once and for all.

So, is there anything “special” about `when:` on handlers? [Hoping for a resounding “No.”]

---

<div class="post-metadata">

**Author:** ![kpfleming](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kpfleming](https://forum.ansible.com/u/kpfleming)\
**Post date:** [October 25, 2023, 3:02pm UTC](https://forum.ansible.com/t/using-when-on-handlers/1952/2 "2023-10-25T15:02:35Z")

</div>

Not to my knowledge, I use `when` in handlers without problems.

---

<div class="post-metadata">

**Author:** ![tanganellilore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tanganellilore/32/178_2.png) [@tanganellilore](https://forum.ansible.com/u/tanganellilore)\
**Post date:** [October 31, 2023, 8:07pm UTC](https://forum.ansible.com/t/using-when-on-handlers/1952/3 "2023-10-31T20:07:29Z")

</div>

Hi,

The only thing to be considered that ‘when’ in a handlers can be risky if variable and/or condition used is not defined when handle is used.

This can be possible if you notify it and playbook (or meta) finished/called before variable is setted.

Apart this, yes when works well.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 30, 2023, 8:07pm UTC](https://forum.ansible.com/t/using-when-on-handlers/1952/4 "2023-11-30T20:07:32Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
