Conditional 'notify' ?

Hi Ansible people !

tl;dr : I’m wondering if there’s an elegant way to conditionally notify a handler (for example, only if it’s actually present as a handler in the role)

Here’s what I’m trying to do :
I have 2 kinds of servers using the same Django codebase : web servers running nginx+gunicorn, and offline tasks processors running a Celery task queue.
I made 3 Ansible roles : one for stuff that’s common to the 2 types of server (including updating the git repo), and 2 for the specific bits of each type.

And I’d like Unicorn to be notified/restarted after a git pull just on the web servers.
The obvious solution seems to be moving the git task out of the common role and into 2 slightly different tasks in each specific role, one with a notify and the other without, but that sounds like a lot of code duplication for such a small difference.

Any better way ?
Thanks !

handlers are notified when a task was changed;
maybe you can tweak your use case using “changed_when:”

You can also assign the conditional to the handler and that also works.

Thanks !

I’ve tried :
when: ‘webservers’ in group_names

in the handler but this gives me a YAML syntax error.
Someone has suggested : when: ‘“webservers” in group_names’ which does seem to work, but I haven’t seen the need to quote conditionals anywhere before. What gives ?

you want webservers to be a string in this context, you quote it so it does NOT get interpolated.

sorry, hit return early, when quoting the start of a line in yaml, it expects the whole line to be quoted, so that is why you need to quote the whole thing, cause you start with a quoted string.

Yep, in fact 1.4 gives you handy syntax tips when this happens.