Ansible Handlers and Notifies

What is the difference between handlers and notifiers, and how do create handlers and notifiers

Hello @surimadhira9,
Handlers are special tasks that only get executed when triggered via the notify directive. And they are executed at the end of the play, once all tasks are finished.
For example, Considering such a scenario, this is how a handler to restart the Nginx service would look like:

...
  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted  

And to trigger this handler, you’ll need to include a notify directive in any task that requires a restart on the Nginx server. using this line notify: Restart Nginx

Thanks,
KodeKloud Support

1 Like

Thanks @Ayman

I understand the concept and can I know where should I add this line notify: Restart Nginx? In the same yaml file or at server end

Hello @surimadhira9,
You add it to any task that requires a restart on the Nginx server. for example, check the below syntax.

- name: <task name>
      ..
      ..
      ..
  notify: Restart Nginx

Thanks,
KodeKloud Support

1 Like