HTTPD SERVICE(IDEMPOTENCE NATURE)

shristi sharma
4 min readMar 21, 2021

--

Restarting HTTPD Service is not idempotence in nature and also consume more resources .

In this blog we will going to rectify this challenge using ansible playbook

ANSIBLE

Ansible is an open source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code .It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft-Windows. It includes its own declarative language to describe system configuration . Ansible was written by Michael DeHaan and acquired by RedHat in 2015. Ansible is agentless, temporarily connecting remotely via SSH(allowing remote PowerShell execution) to do its tasks.

Ansible is a tool that provide us declarative language.

HANDLERS

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. Each handler should have a globally unique name.

NOTIFY

Handlers are just like regular tasks in an Ansible playbook (see Tasks), 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.

ANSIBLE IDEMPTOENCE NATURE

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.

Ansible didn’t run the run the task again and again if it is already exist means it will not run the same thing again and again for you

Let’s perform the practical and see the implementation or use-case of notify and handlers

First create a directory/workspace where you will write all the code of the task give it any name of your choice.

Then inside the workspace create a yml file named it as handlers in my case

create a directory

Make this configuration file in the workspace you have created I name it as shr.conf.j2 it will be used in the code

make a shr.conf.j2

Let’s write a code in ansible playbook

OUTPUT

As we can see the output something has changed in the configuration file that’s why the output is shown in yellow color

As now lets do one thing again run the playbook and see the output

As we can see after running the playbook nothing has changed because no file has been updated so the output is in green color.

Now’s let again see the practical and changed something in the configuration file shr.conf.j2 .

What I have done I have changed the confg file first it is 8080 and changed to 8081 .

Let’s run the playbook again and see the output

As we can see again something has changed in the file notify the handlers and httpd services has been restarted again .

Hence we rectify this challenge and also changed the behavior of HTTPD using the concept of handlers

Go through my blog if don’t understand anything ping me anytime on LinkedIn.

BY: Shristi Sharma:)

--

--

shristi sharma

Aspiring Devops Enthusiastic |Content Creator |Python | Machine Learning | Cloud Computing