# how to collapse multiple restarts?

**URL:** <https://forum.ansible.com/t/how-to-collapse-multiple-restarts/15578>\
**Category:** Ansible Project\
**Created:** [June 30, 2014, 11:12pm UTC](https://forum.ansible.com/t/how-to-collapse-multiple-restarts/15578 "2014-06-30T23:12:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillip.corchary](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@phillip.corchary](https://forum.ansible.com/u/phillip.corchary)\
**Post date:** [June 30, 2014, 11:12pm UTC](https://forum.ansible.com/t/how-to-collapse-multiple-restarts/15578/1 "2014-06-30T23:12:15Z")

</div>

In the snippet below, Am I right that elastic serach would be restarted 3 times assuming each action is performed?

Is there a way to collapse this into just one restart?

Say some way to set a flag for each action if the action is executed, and the at the end run a conditional to check if the flag \>1 and then run the restart handler?

- name: elasticsearch | write java server wrapper from template, restart ES  
action: template src=templates/elasticsearch\_service\_wrapper.j2 dest=${deploy\_base}/elasticsearch-${elasticsearch.version}/bin/service/elasticsearch mode=0755  
tags:

- configuration  
notify:

- **Restart elasticsearch**

- name: elasticsearch | Install config from templates, then restart ES  
action: template src=templates/$item dest=${deploy\_base}/elasticsearch-${elasticsearch.version}/config/$item mode=0644 owner=root group=root  
with\_items:

- logging.yml

- elasticsearch.yml  
tags:

- configuration  
notify:

- **Restart elasticsearch**

- name: elasticsearch | Install upstart start script (conf) from templates, then restart ES  
action: template src=templates/elasticsearch.conf.initd.j2 dest=/etc/init/elasticsearch.conf  
notify:

- **Restart elasticsearch**

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 30, 2014, 11:20pm UTC](https://forum.ansible.com/t/how-to-collapse-multiple-restarts/15578/2 "2014-06-30T23:20:26Z")

</div>

“In the snippet below, Am I right that elastic serach would be restarted 3 times assuming each action is performed?”

Nope.

Ansible will only run handlers in certain places (between pre\_tasks and roles/tasks, and before post\_tasks), unless there is a task that says

“- meta: flush\_handlers”.

This is an easy experiment to try on your own and you should observe this.

pre\_tasks and post\_tasks are mainly of use for rolling update plays with load balancer or monitoring remove/add type steps, and can usually be ignored if you are not doing these things.

---

<div class="post-metadata">

**Author:** ![phillip.corchary](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@phillip.corchary](https://forum.ansible.com/u/phillip.corchary)\
**Post date:** [June 30, 2014, 11:24pm UTC](https://forum.ansible.com/t/how-to-collapse-multiple-restarts/15578/3 "2014-06-30T23:24:36Z")

</div>

Oops. My mistake.

I should RTFM.  
\<\<

Handlers are lists of tasks, not really any different from regular tasks, that are referenced by 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.

> >

Thanks Michael
