Ansible not running handlers from included roles

ansible

I'm trying to run notify on a handler in the same role and that role is being included as a dependency.

Here's my playbook

root@monitor:/etc/ansible# cat monitor.yml
---

- hosts: local
  connection: local
  become: yes
  become_user: root
  roles:
    - common
    - role: sensu
      sensu_install_server: true

My common files look like

root@monitor:/etc/ansible# cat roles/common/tasks/main.yml
- name: Add the OS specific variables
  include_vars: '{{ ansible_os_family }}.yml'

#
# ansible run script
- name: copy the ansible-run script
  copy:
    src=ansible-run.py
    dest=/usr/bin/ansible-run
    owner=root
    group=root
    mode=0700
- cron: name="cron ansible-run" minute="*/5" job="/usr/bin/ansible-run > /dev/null 2&1"


#
# ntp
- name: Install ntp
  package: name=ntp state=present
- name: Start/stop ntp service
  service: name={{ ntp_service_name }} state=started enabled=yes



root@monitor:/etc/ansible# cat roles/common/meta/main.yml
---
dependencies:
  - { role: users }
  - { role: sensu }

My sensu role is where I want the notify to work in the tasks/client.yml

root@monitor:/etc/ansible# cat roles/sensu/tasks/main.yml

- include: common.yml

- include: server.yml
  when: sensu_install_server
  ignore_errors: true

- include: client.yml





root@monitor:/etc/ansible# cat roles/sensu/tasks/client.yml
- name: reload ansible_local
  setup: filter=ansible_local

- name: copy plugins files
  copy:
    src=files/sensu/plugins/
    dest=/etc/sensu/plugins/
    owner=sensu
    group=sensu
    mode=0750
  notify:
    - restart sensu client


- name: generate config files
  template:
    src=client.json.j2
    dest=/etc/sensu/conf.d/client.json
    owner=sensu
    group=sensu
    mode=0640
    backup=yes
  notify:
    - restart sensu client

- name: enable sensu-client to survive reboot
  service:
    name=sensu-client
    enabled=yes
    state=started



root@monitor:/etc/ansible# cat roles/sensu/handlers/main.yml
---
# handlers file for sensu
- name: restart sensu server
  service: name=sensu-{{ item }} state=restarted
  with_items:
    - server
    - api

- name: restart sensu client
  service: name=sensu-client state=restarted

- name: restart uchiwa service
  service: name=uchiwa state=restarted

- name: restart nginx service
  service: name=nginx state=restarted

A look from a run shows the client.json gets changed but I never see a notify for the handler being called.

Best Answer

First of all, because of the wrong file type, your handlers are getting ignored I suppose:

roles/sensu/handlers/main.html

Should be: roles/sensu/handlers/main.yml

Secondly, please note, although you have a couple of handlers, the only handler that is notified by the notifiers is: restart sensu client. So the other handlers won't get run.

Handlers are lists of tasks, not really any different from regular tasks, that are referenced by a globally unique name. Handlers are what notifiers notify. If nothing notifies a handler, it will not run. Regardless of how many things notify a handler, it will run only once, after all of the tasks complete in a particular play.

Ref: http://docs.ansible.com/ansible/playbooks_intro.html