RhythmicDevil
RhythmicDevil

Reputation: 835

Why are my Ansible handlers not firing?

I have a playbook that installs tomcat and then deploys some web applications. The web application deploy task(s) notifies a handler to restart tomcat. But the handler never fires. I am using a handler to manage the tomcat service because I understand from the docs that handlers should only fire once even if called multiple times. Am I missing something obvious?

This is the playbook:

---
- hosts: all
  become: true
  become_user: root
  roles:
  - role: common
  - role: nginx
  - role: tomcat
  - role: launchpad
  - role: manager
  - role: reporting
  handlers:
  - include: /tomcat/handlers/etitomcat_service_ctrl.yml

This is one of the roles that deploys the web app:

---
- name: Remove the current installation of LaunchPad
  file: path={{etitomcat_path}}/webapps/{{launchpad_module}} state=absent

- name: Remove the current war file for {{launchpad_module}}
  file: path={{etitomcat_path}}/webapps/{{launchpad_module}}.war state=absent     

- name: Download the latest snapshot of LaunchPad and deploy it to {{etitomcat_path}}
  get_url: url={{launchpad_source_url}} dest={{etitomcat_path}}/webapps/{{launchpad_module}}.war mode=0744 owner={{etitomcat_user}} group={{etitomcat_group}} force=yes
  notify: "restart_eti_tomcat"

This is the handler:

  - name: "Restart ETI Tomcat"
    service: name=etitomcat state=restarted
    become: true
    become_user: root
    listen: "restart_eti_tomcat"

  - name: "Start ETI Tomcat"
    service: name=etitomcat state=started
    become: true
    become_user: root
    listen: "start_eti_tomcat"

  - name: "Stop ETI Tomcat"
    service: name=etitomcat state=stopped
    become: true
    become_user: root
    listen: "stop_eti_tomcat" 

Upvotes: 4

Views: 13444

Answers (3)

Gesias
Gesias

Reputation: 778

This may be beside the point but I'll add this regardless as the question headline is rather wide and this is the question I found when googling and below is the solution for the particular issue I had.

Do take into consideration that the handlers only triggers when there is a change registered in the corresponding task. Even if you run the play with the highest verbosity level there will be NO entry like this which spells this out.

RUNNING HANDLER [base : somehandler ] *********************** 
Unchanged:  Skipping

And when they are triggered after a change it will be after all the tasks has been performed.

This really did my head in since the tasks notified you regardless of if they really did something or not whereas handlers stays quiet.

For example, if you have a task A that you have been running a few times until it works like you expect.

Then after that connect a handler B to restart the service, nothing will happen unless you wipe the operation the task A are doing or change it.

As long as task A registers no change it will not trigger handler B.

This is the behavior for ansible 2.2.1 anyways.

Upvotes: 5

Henrik Pingel
Henrik Pingel

Reputation: 3193

Adding static: yes should resolve this issue when using Ansible >= 2.1.

handlers:
- include: /tomcat/handlers/etitomcat_service_ctrl.yml
  static: yes

Take a look at this Github issue, the linked google groups thread might contain valuable information as well.

edit

As pointed out by @rhythmicdevil the documentation notes:

You cannot notify a handler that is defined inside of an include. As of Ansible 2.1, this does work, however the include must be static.

Upvotes: 7

Arbab Nazar
Arbab Nazar

Reputation: 23791

You can add the tasks and post_tasks section instead of handlers, hope it will work for you:

---
- hosts: all
  become: true
  become_user: root
  tasks:
  - role: common
  - role: nginx
  - role: tomcat
  - role: launchpad
  - role: manager
  - role: reporting
  post_tasks:
  - include: /tomcat/handlers/etitomcat_service_ctrl.yml

Upvotes: -1

Related Questions