# Conditional 'notify' ?

**URL:** <https://forum.ansible.com/t/conditional-notify/13835>\
**Category:** Ansible Project\
**Created:** [November 4, 2013, 10:47am UTC](https://forum.ansible.com/t/conditional-notify/13835 "2013-11-04T10:47:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renaud\_Guerin](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@Renaud\_Guerin](https://forum.ansible.com/u/Renaud_Guerin)\
**Post date:** [November 4, 2013, 10:47am UTC](https://forum.ansible.com/t/conditional-notify/13835/1 "2013-11-04T10:47:12Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [November 4, 2013, 6:55pm UTC](https://forum.ansible.com/t/conditional-notify/13835/2 "2013-11-04T18:55:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 4, 2013, 10:59pm UTC](https://forum.ansible.com/t/conditional-notify/13835/3 "2013-11-04T22:59:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Renaud\_Guerin](https://avatars.discourse-cdn.com/v4/letter/r/e9bcb4/32.png) [@Renaud\_Guerin](https://forum.ansible.com/u/Renaud_Guerin)\
**Post date:** [November 6, 2013, 3:09pm UTC](https://forum.ansible.com/t/conditional-notify/13835/4 "2013-11-06T15:09:22Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 6, 2013, 4:43pm UTC](https://forum.ansible.com/t/conditional-notify/13835/5 "2013-11-06T16:43:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 6, 2013, 4:44pm UTC](https://forum.ansible.com/t/conditional-notify/13835/6 "2013-11-06T16:44:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 10, 2013, 9:17pm UTC](https://forum.ansible.com/t/conditional-notify/13835/7 "2013-11-10T21:17:14Z")

</div>

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