Using "when:" on handlers

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

# 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.”]

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



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.

1 Like

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