38

I have a task:

- name: uploads docker configuration file
  template:
    src: 'docker.systemd.j2'
    dest: '/etc/systemd/system/docker.service'
  notify:
    - daemon reload
    - restart docker

in Ansible playbook's documentation, there is a sentence:

Notify handlers are always run in the order written.

So, it is expected, that daemon reload will be ran before restart docker, but in logs, i have:

TASK [swarm/docker : uploads docker configuration file] ************************
…
NOTIFIED HANDLER daemon reload
NOTIFIED HANDLER restart docker
…
RUNNING HANDLER [swarm/docker : restart docker] ********************************
…
RUNNING HANDLER [swarm/docker : daemon reload] *********************************
…

There are no more "NOTIFIED HANDLER" in logs. Can anyone explain, what i'm doing wrong? :(

1
  • 9
    Looks like the documentation has been updated: "Notify handlers are always run in the same order they are defined, not in the order listed in the notify statement."
    – JCotton
    Commented Apr 14, 2016 at 0:11

2 Answers 2

75

I think you may have “restart docker” listed before “daemon reload” in your handlers file.

That part of the ansible documentation is a bit misleading. It means that handlers are executed in the order they are written in the handlers file, not the order they are notified.

This is little more clear in the documentation:

Handlers always run in the order they are defined, not in the order listed in the notify-statement. This is also the case for handlers using listen.

1
  • It's worth noting that if you are working with multiple roles and you do an include_role task, it counts all of that role's handlers as being defined later than everything else to that point. I am not sure about whether import_role task has a different behavior here, but I am 99% certain that if you just list your roles in the roles section of your playbook, the handler definition order will match the order you listed your roles out.
    – 2rs2ts
    Commented Nov 15, 2023 at 21:00
9

I just figured out that I can have handlers call other handlers.

Example task:

- name: Configure Apache
  copy: src=apache-azkaban.conf dest=/etc/apache2/sites-enabled/azkaban.conf
  notify:
   - a2enmod proxy
   - a2enmod proxy_http

In my handlers/main.yml:

- name: a2enmod proxy
  shell: a2enmod proxy
  notify:
    - restart apache2

- name: a2enmod proxy_http
  shell: a2enmod proxy_http
  notify:
    - restart apache2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.