Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force handler to run before executing a task in Ansible?

I have a playbook which should configure on specified IP, and than connect to this app to configure stuff inside.

I've got a problem: I need to restart app after I've changed anything in app config, and if I do not restart app, connection to it failed (no connection because app knows nothing about new config with new IP address I'm trying to access).

My current playbook:

tasks: - name: Configure app   template: src=app.conf.j2 dest=/etc/app.conf   notify: restart app  - name: Change data in app   configure_app: host={{new_ip}} data={{data}}  handlers: - name: restart app   service: name=app state=restarted 

I need to force the handler to run if configure_app changed before executing 'Change data in app'.

like image 463
George Shuklin Avatar asked Dec 01 '15 11:12

George Shuklin


People also ask

How do you trigger handlers in Ansible?

To trigger this handler, you'll need to include a notify directive in any task that requires a restart on the Nginx server. The following playbook replaces the default document root in Nginx's configuration file using the built-in Ansible module replace.

Which module can be used to force a handler to run in between two tasks in Ansible?

If you need handlers to run before the end of the play, add a task to flush them using the meta module, which executes Ansible actions: tasks: - name: Some tasks go here ansible.

What is difference between tasks and handlers in Ansible?

Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify keyword and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.

How do I make an Ansible task run only once?

For such requirements where we need one tasks to run only once on a batch of hosts and we will be running that from Ansible controller node, we have feature parameter named run_once. When we have this parameter mentioned in a task, that task will run only once on first host it finds despite the host batch.

What are handlers in Ansible and how to use them?

Handlers in Ansible are special tasks that only get executed when triggered by another tasks via the notify directive. By default, handlers run not immediately but only after all the tasks in a particular play have been completed. This makes sense, as the handler will only run one time, regardless of how many tasks have notified it.

What happens when an ansible task fails?

Handlers and failure ¶ Ansible runs handlers at the end of each play. If a task notifies a handler but another task fails later in the play, by default the handler does not run on that host, which may leave the host in an unexpected state. For example, a task could update a configuration file and notify a handler to restart some service.

How to run httpd service from Ansible playbook?

We will write a playbook ansible-handlers.yml to install httpd package and then use a handler to start the httpd service. This handler will be executed only when change is made i.e. the dependent task reports changed=1 or higher value. You have to take care of the indentation for handlers as it should start from the same line as your tasks.

How to avoid unnecessary restarts of Apache in Ansible?

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. In this sample playbook I have 2 tasks which will print some text on the console using debug module.


1 Answers

If you want to force the handler to run in between the two tasks instead of at the end of the play, you need to put this between the two tasks:

- meta: flush_handlers

Example taken from the ansible documentation :

tasks:    - shell: some tasks go here    - meta: flush_handlers    - shell: some other tasks 

Note that this will cause all pending handlers to run at that point, not just that specific one.

like image 191
Woodham Avatar answered Oct 25 '22 02:10

Woodham