Conditional handlers from a task

79 views
Skip to first unread message

Samnang Sen

unread,
Jun 23, 2015, 10:46:03 AM6/23/15
to ansible...@googlegroups.com
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"



Stephen Granger

unread,
Jun 23, 2015, 11:48:42 AM6/23/15
to ansible...@googlegroups.com
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


There maybe other ways to do it though.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9192b3db-e42d-4b2e-bd6a-b688a2f1aa3f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve

Samnang Sen

unread,
Jun 23, 2015, 12:14:27 PM6/23/15
to ansible...@googlegroups.com
I'm such an idiot, "result|failed" works like a charm. I kept reading the "|" as an "OR" statement. Ugh.

Stephen Granger

unread,
Jun 23, 2015, 2:42:30 PM6/23/15
to ansible...@googlegroups.com
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.


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


For more options, visit https://groups.google.com/d/optout.



--
Steve
Reply all
Reply to author
Forward
0 new messages