Making HTTPD Service Idempotent Using Ansible

Piyush Bagani - Mar 26 '21 - - Dev Community

Hello Readers😊,

After Reading This Article, You will be able to make the httpd service idempotent using Ansible. You can Read my previous article to know more about Ansible.
Link: https://dev.to/piyushbagani15/ansible-in-action-how-aws-is-solving-challenges-using-ansible-oll

So, without any further delay let’s get started.

What is Idempotence??

In general, Idempotence is “the property of certain operations in mathematics and computer science that can be applied multiple times without changing the result beyond the initial application”. In Ansible it means after running a playbook to set things to a desired state, further runs of the same playbook should result in no changes.

Let’s say we ran an ansible-playbook previously and did some changes afterwards but if some service like httpd is started in previous run is started, according to particular task written then it won’t restart in next run and hence the changes made had no effect on the system. Also When we run ansible-playbook for restarting the service of httpd. It will again restart the service at the target node but we want the service to only restart when I make any change to configuration files otherwise it will not restart the service of httpd and save the compute resources.

To solve this kind of Use case we use handlers and notify in Ansible.

Handlers in Ansible

Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified.

So We can Write the Ansible Playbook to demonstrate the use of handlers and making httpd service idempotent.
You can see the ansible-playbook by clicking on the link provided below.
Link: https://github.com/PiyushBagani15/Ansible_handlers
After Running this playbook, it is clearly visible that We can restart the httpd service in an idempotence nature.

I am attaching some images to see how this playbook runs.
alt text
alt text
alt text
Mainly,We have configured Apache Webserver using Ansible and made the httpd service idempotent. The Final Result we get after visiting the WebPage is as follows:
alt text

So, That’s It. Thank You Everyone for Reading.

Keep Learning, Keep Hustling🎯

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .