# how do fire handler with condition?

**URL:** <https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950>\
**Category:** Ansible Project\
**Created:** [August 5, 2015, 10:43am UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950 "2015-08-05T10:43:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ichiro\_MATSUNAGA](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@Ichiro\_MATSUNAGA](https://forum.ansible.com/u/Ichiro_MATSUNAGA)\
**Post date:** [August 5, 2015, 10:43am UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/1 "2015-08-05T10:43:15Z")

</div>

Hi

I want to fire handler with condition.  
for example, when change httpd.conf, i want to restart apache on staging, bun not want on production.

what should i do?

```auto
inventory/staging
httpd_restart=true

inventory/production
httpd_restart=false

tasks:
# i want to fire when inventory is staging
# but not fire when inventory is production
- copy: src=httpd.conf dest=/etc/httpd/httpd.conf
handler:
- service name=httpd state=restart

```

thanks.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [August 5, 2015, 2:26pm UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/2 "2015-08-05T14:26:39Z")

</div>

It's a tricky one - you probably don't want production bouncing  
services all day long  
but on the other hand I doubt you never want to pick up any config  
changes either.

One day you'll have a power outage or something and that's a bad time  
to find out your  
configuration file is actually invalid.

That said, we do occasionally do this against prod. if we've tested in  
other envs and really  
do need to avoid a restart.

( A good example would be something we can dynamically add to a  
service configuration by  
hand at runtime without a restart, but we also want to ensure the  
on-disk config is up to date).

If we don't want a service to bounce we

\* do a 'dummy run' of ansible (-C) with the -D (diff) option  
\* see what Ansible would change  
\* make those changes by hand

that way the handlers don't fire, since Ansible didn't change the  
files contents.

This is a hassle, but since we don't want to encourage this behaviour  
that's fine 🙂

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [August 6, 2015, 8:54am UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/3 "2015-08-06T08:54:48Z")

</div>

We have this requirement as well. We just put a when condition on the task in the handler to only restart the service if it’s not prod. You need to have the environment in a variable for this to work. e.g. this may not be the 100% correct syntax as I’m not at work, but we do something like:

handler:

- service: name=httpd start=restarted  
when: {{environment}} != ‘prod’

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 6, 2015, 3:09pm UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/4 "2015-08-06T15:09:42Z")

</div>

when: "'{{environment}}' != 'prod'"

also i would not use environment as a variable as it can conflict with  
the keyword.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 6, 2015, 3:10pm UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/5 "2015-08-06T15:10:56Z")

</div>

actually, let me correct myself, no mustaches needed in when:

when: environment != 'prod'

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [August 6, 2015, 10:18pm UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/6 "2015-08-06T22:18:47Z")

</div>

Agreed, “environment” is probably a bad choice of variable names. We actually use a variable called “node\_environment”.

---

<div class="post-metadata">

**Author:** ![Ichiro\_MATSUNAGA](https://avatars.discourse-cdn.com/v4/letter/i/aeb1de/32.png) [@Ichiro\_MATSUNAGA](https://forum.ansible.com/u/Ichiro_MATSUNAGA)\
**Post date:** [August 7, 2015, 5:06am UTC](https://forum.ansible.com/t/how-do-fire-handler-with-condition/18950/7 "2015-08-07T05:06:35Z")

</div>

hi.

thaks for many answer.

i feel ugly a little for use ‘when’ (thanks Paul),  
and feel tricky to fire handler with condition (thanks Dick).

i decide to restart service all enviroments.

thanks.

2015年8月5日水曜日 22時27分57秒 UTC+9 Ichiro MATSUNAGA:
