# Conditional handlers from a task

**URL:** <https://forum.ansible.com/t/conditional-handlers-from-a-task/18581>\
**Category:** Ansible Project\
**Created:** [June 23, 2015, 2:46pm UTC](https://forum.ansible.com/t/conditional-handlers-from-a-task/18581 "2015-06-23T14:46:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [June 23, 2015, 2:46pm UTC](https://forum.ansible.com/t/conditional-handlers-from-a-task/18581/1 "2015-06-23T14:46:03Z")

</div>

FYI, I’m running my Ansible playbook in “pull-mode” not push mode. Therefore, my nodes will publish the results of their task via Hipchat.

With that said, I have a task that installs RPMs. When the installs are successful, the nodes notify via Hipchat that the task was successfully run. Now, in the event that a task fails, i force it to notify hipchat w/ the “–force-handlers” paramter. My question, is there a way to display a message according to whether it fails or runs successfully?

If I had to, I don’t mind having multiple handlers because I would need to modify the color for fail tasks to red as suppose to the default yellow or green for a successful run.

This works great

# Task

- name: Install Perl modules  
command: sudo rpm -Uvh {{ rpm\_repository }}/{{ item.key }}-{{ item.value.svn\_tag }}.rpm --force  
with\_dict: deploy\_modules\_perl  
notify: announce\_hipchat

# Handler

- name: announce\_hipchat  
local\_action: hipchat  
from=“deployment”  
token={{ hipchat\_auth\_token }}  
room={{ hipchat\_room }}  
msg=“[{{ ansible\_hostname }}] Successfully installed RPMs!”  
validate\_certs=“no”

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [June 23, 2015, 3:48pm UTC](https://forum.ansible.com/t/conditional-handlers-from-a-task/18581/2 "2015-06-23T15:48:38Z")

</div>

I’d use register for the first task and just use the when conditional.

- name: Install rpm  
yum: name=  
sudo: true  
ignore\_errors; yes  
register: yum\_install

- name: Notify Hipchat of success  
hipchat: …  
color: green  
when: yum\_install|success

- name: Notify Hipchat of failure  
hipchat: …  
color: red  
when: yum\_install|failed

[https://docs.ansible.com/playbooks\_conditionals.html](https://docs.ansible.com/playbooks_conditionals.html)

There maybe other ways to do it though.

---

<div class="post-metadata">

**Author:** ![Sam\_S](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sam\_S](https://forum.ansible.com/u/Sam_S)\
**Post date:** [June 23, 2015, 4:14pm UTC](https://forum.ansible.com/t/conditional-handlers-from-a-task/18581/3 "2015-06-23T16:14:27Z")

</div>

I’m such an idiot, “result|failed” works like a charm. I kept reading the “|” as an “OR” statement. Ugh.

---

<div class="post-metadata">

**Author:** ![viper233](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/viper233/32/2060_2.png) [@viper233](https://forum.ansible.com/u/viper233)\
**Post date:** [June 23, 2015, 6:42pm UTC](https://forum.ansible.com/t/conditional-handlers-from-a-task/18581/4 "2015-06-23T18:42:25Z")

</div>

It just came to me why you would be using handlers and it (maybe) makes sense when you don’t what to have the hipchat task run every time as the rpm state may not change.

You could probably get around this though by using tags to have these as deploy steps.

[http://docs.ansible.com/playbooks\_tags.html](http://docs.ansible.com/playbooks_tags.html)

Your workflow may not require this though as you are using ansible-pull.
