how to collapse multiple restarts?

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!

“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.

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