Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of notify handlers

Tags:

ansible

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? :(

like image 249
arz.freezy Avatar asked Feb 01 '16 12:02

arz.freezy


People also ask

Can a handler notify another handler?

Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it. For example, if multiple tasks update a configuration file and notify a handler to restart Apache, Ansible only bounces Apache once to avoid unnecessary restarts.

What's the Ansible playbook execution order?

Playbook execution. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.

Where do you put handlers Ansible?

In a nutshell, handlers are special tasks that only get executed when triggered via the notify directive. Handlers are executed at the end of the play, once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services.

What are the handlers in Ansible?

In Ansible, a handler is just like any other task but only runs when called or notified. It takes action when a change has been made on the managed host.


2 Answers

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 glossary

like image 196
omame Avatar answered Sep 19 '22 08:09

omame


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 
like image 22
dannyman Avatar answered Sep 19 '22 08:09

dannyman