In this video, we are going to learn ansible handlers.
Handlers are executed at the end of the play.
Once all tasks are finished and ansible handlers are typically used to start, reload, restart and
stop services.
So sometimes you want to run a task only when a change is made on a machine.
For example, you may want to restart a service if a task updates a configuration file of that service,
but if not, the configuration file will be unchanged.
All right.
Remember the case when we had to reload the firewall DX because we wanted to enable HTTP service?
Yes, that's a perfect example of using Handler.
If you had not updated or added the HTTP service in firewall dx, then there was no need to reload the
firewall service.
So basically handlers are tasks that only run when notified.
So pay close attention.
The notified is something that has to be inside of the playbook that will indicate that the handler
should be run at the end of the playbook.
So each handler should have a globally unique name.
All right.
Let's look at look at the example of our first handler playbook.
All right.
Here we have the example.
If you pay close attention right here, name, verify, Apache installation.
So this is the name of the playbook.
It's going to run against the local host.
And the task, the very first task is ensure Apache is at latest version.
All right.
So to ensure that we'll do the job module, the name of the package will be HTTP and the state is going
to be the latest one.
So this latest option will ensure that it has Apache package and it is the latest one.
Then the second task will copy updated Apache config file.
So you might have the updated config file.
So you will use the module copy.
The source will be temp, http, dcop or wherever you have the updated http file located.
Then the destination will be of course an http dhcp, which is the default location.
Or maybe if you have changed the default location of Apache somewhere else, then you specify right
here, now comes the notify.
So notify is is the indicator right here that it requires Apache to be restarted name.
That's the third task.
Ensure Apache is running and you know how we have to define to have it running then comes in the last
part is the handler and the handler is what name restart Apache the service named DPD and the state
can be restarted.
Now if you notice this restart Apache, it will restart the Hpdi toward the end of the playbook.
And the reason we are asking it to do is because it is updating the HTTP file.
So before it goes and and started first, it will ensure it is up and running and then it will go ahead
and restart it.
That's what the handlers are for.
Next, we will take an example of our firewall.
The playbook here is the firewall playbook and the name of the firewall.
The playbook is enable service on firewall DX.
It will run against local host and the very first task would be open the port for HTTP.
The module will be firewall DX, the service will be http permanent run it about time and the state
to enable it right now now comes in the notify part you want to use the notifier handler here where
it will reload the firewall.
All right.
So the reason we are putting this here is because we don't want to reload right away, because we wanted
to first ensure the firewall is running or not.
So that's where we are just putting notify here.
So it could be reloaded at the end after we ensure the firewall is running.
All right.
So we ensure it.
It is up and running and started now comes in the handler and handler is saying to reload the firewall
D which we did the notification here and the service and the name of the service is Firewall DX and
the State is Reloaded.
That's a real example, which we did previously for our firewall, and that's a perfect example to use
our handlers.