Let’s make Apache webserver services idempotence using Ansible handlers

The title seems ambiguous. Арасhe httрd is а serviсe whiсh helps in deploying web server on the system. The next eye-саtсher is Idemроtenсe.

In this article, I would be achieving Apache webserver restart service with the ability of idempotence using ansible handlers

The main reason for using ansible is Idempotence as by the ability os idempotence ansible save a lot of CPU time which can be get wasted, think of the situation of where we have to scale our server and we have run the same playbook where all the server are added so is the ansible will run the task on the system which are already configured?. The answer is no as it has the intelligence of Idempotence which will guide it whether the task is already been run on that system or not leads to saving so much the resources of the company.

But there is some situation where ansible can’t able act Idempotence, For example, while restarting the httpd service it does not follow idempotence. So for such a task, we make ansible act like Idempotece by using Handlers.

If you don’t know how to set up an apache web server using ansible follow this article: https://agarwalyash02.blogspot.com/2020/12/automating-apache-webserver.html

Firstly we will run our playbook and check what’s the issue

So as we can see we have not changed anything in the configuration file still it is restating the services this is a waste of resources as the CPU has to give its time to restart the httpd services which we don't want

So here the challenge is restarted keyword as whenever playbook run that many time restart will restart the httpd services irrespective weather we made a change or not.

So for this, we will use the concept of Handlers

Handlers will play a vital role in restarting the httpd server. when there is some change in the code then only the apache webserver will restart again. This will reduce the time taking by other servers.

Now let's implement this concept in our playbook and let's see what happens

Here we put the service of restarting the server under handlers. Also, we have used the keyword notify. The notify keyword is used to handle the handlers i.e it notifies the handlers of any new changes made. It’s also noticeable that we write the handler name in the notify keyword so that it notifies that particular handler something is changed in the file and restart of the server is required.

Now let’s run the playbook and check whether this thing is working or not

Here we can see that we have not made any change in the configuration file and the service is also not restarted, the best part is ansible won't include that task in the list also as for ansible that restarts task doesn't exist.

But if we made a change in the file then what will happen does restart work lets check by running the playbook again with some small changes in the file.

We can see that we have changed some content in the file now my services gets restarted, now we can say my restart service of the webserver is behaving in an idempotent manner.

Here is the Github link to the playbook used.

So in today's article, we have seen the power and intelligence of Ansible and draws us to the conclusion that ansible-handlers are a way to achieve the idempotency in restarting the httpd service.

Hopefully, you learn something new from the article as well as enjoy it.

Thanks for reading this article! Leave a comment below if you have any questions.

Comments

Popular posts from this blog

How to configure Hadoop cluster using Ansible playbook

Automation Using Python Menu-Driven Program

Configuring HAProxy using Ansible playbook