Let’s make Apache webserver services idempotence using Ansible handlers

Image for post
Image for post

Ansible is a tool which is used for Configuration Management. In Ansible we only declare “what to do”. We don’t need to specify “How to do” because Ansible already knows that on each type of Operating System as Ansible is a Declarative language. This intelligence of Ansible comes from modules. Modules know “How to do operate on different OS”. We don’t require to give a particular OS command. Ansible doesn’t perform any operation on the OS. It is done by respective OS command. Ansible module knows which command is required to run on a specific OS.

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

The above line seems obscure. Well, Apache httpd is a service offered by the software which is a foremost necessity when you deploy a webserver in your system. The next eye-catcher is Idempotence.

The principle that enables Ansible to be declarative and yet reliable is idempotence, a concept borrowed from mathematics. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application, such as multiplication by zero. Ansible modules are idempotent. For example, if one of the tasks is to create a directory on the server, then the directory will be created if and only if it does not already exist. This sounds pretty simple to implement in the case of filesystem operations, but it’s not as trivial when considering more complex modules.

Idempotence is very important to DevOps tools like Ansible because we can check the configuration state and only make changes when necessary. This saves both resources as well as time.

Ansible by default follows idempotence with most of the tasks. But we some of the tasks it does not follow it. For example, while restarting httpd service it does not follow idempotence. So for such cases, we use Handlers.

If you don’t know how to set up apache webserver using ansible follow this article: https://agarwalyash377.medium.com/automating-apache-webserver-configuration-on-docker-container-using-ansible-ee6ea623436a

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

Image for post
Image for post

So as we can see we have not changed anything in the configuration file still it is restating the services this is the waste of resources as 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 are just like regular tasks in an Ansible playbook but are only run if the Task contains a notify directive 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.

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

Image for post
Image for post

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 weather this thing is working or not

Image for post
Image for post

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.

Image for post
Image for post

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 of the playbook used.

This draws us to the conclusion that ansible-handlers are a way to achieve the idempotency in restarting the httpd service. Handlers check whether any new changes have made. If not, then no changes are made. If yes, then the service is restarted.

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store