To make HTTPD Service idempotence in Nature using Ansible

Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in the Ansible playbook.

Today we will answer above problem so let’s start it.

Idempotence in Ansible

Handlers is used here to make Httpd service Idempotence in nature.

Handlers are just like normal tasks in an Ansible playbook but they run only when if the Task contains a “notify” directive. It also indicates that it changed something

So firstly lets configure our whole setup..

We are running two instances on virtual box

  • rhel8_arth —( Controller Node
  • rhel exp 2–( Target Node

Below is script that I have written. Github link for the same —

index.html files is

Before running our yml file on controller node just checking httpd is not installed on target node by the command —

systemctl status httpd
Httpd server is not there on target node

Running the yml file on controller node —

Now going to target node and checking that httpd server is live now or not —

Httpd server is live now on target node

Now if I run yml file again without any change in index.html file the out put will be —

Notice that Handlers will not run as there is no change in index.html file

But if we change the index.html as

And now we will again run yml file on controller node then handlers will run as there is change in index.html file

This is how we will make httpd service idempotence in nature

Thank you!!

Harsh Agrawal