Conditional handlers from a task

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”

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

There maybe other ways to do it though.

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

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

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