Ansible and the Handlers…

Arjun Nigam
3 min readDec 9, 2020

Sometimes using Ansible playbook ,you come with some issues and one such issue people face in ansible is restarting HTTPD Service is not idempotence in nature and also consume more resources , SO how can we rectify this challenge in Ansible playbook.

The Answer :Handlers

But before telling you about Handlers, i know most of you are much familiar with what is Ansible but for the one’s who aren’t . Let’s give some meaning to this word : Ansible

Ansible

Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.

You can find more on it on my previous blog on Ansible where I have talked more on it…

https://arjunnigam10.medium.com/ansible-and-todays-world-86b31edc290e

Now that you are familiar with what is Ansible , let me tell you about Handlers.

Handlers:

Sometimes you only want a task to be run when a change has taken place . Like: starting your Apache Webserver but often you may have noticed , even if you haven’t changed anything , the task of starting service takes place. So here comes the role of Handlers. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified.

To get a clear picture of this ,have a look at this playbook without the Handlers.

Now if you run it you will observe in the image below that the web server task has again been started(change has been made) instead of being already started .

Now let’s add Handlers to it:

Now again let’s try to run and check whether this has solved our issue or not.

And as you can observe in the image below NO CHANGE(changed = 0) has been made .

So we can say we were successful in overcoming this challenge with the help of Handlers.

Well that was all from my side. Hope it was beneficial for you all…

View the Playbook on Github:

https://github.com/Arjun0071/handlers

Arjun Nigam

https://www.linkedin.com/in/arjun-nigam-4728b11b8

Signing Off!!!

--

--