🔻 Restarting HTTPD Service is not idempotence in nature The Problem and Solution. 🔻

Uditanshu pandey
3 min readApr 14, 2021

Hello Learners In this article we will be solving one of the problems in Ansible.

The problem is

If we Restart the HTTPD Service then it is not idempotence in nature and also consumes more resources.

Idempotence means after the first run of a playbook to set things to the desired state, further runs of the same playbook should result in 0 changes. In simplest terms, idempotency means you can be sure of a consistent state in your environment.

To solve this we will be using Handlers.

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

Let’s start doing tasks step by step.


We will be creating a Host file also known as Inventory. In Inventory, we will define or managed node IP address, user name, and password.


Now, we will write a playbook for the solution.

First, we will install the httpd service on the managed node.

- hosts: web
- name: Installation
name: httpd
state: present

This block of code will install the httpd package on the managed node.

Now, we will copy the content in the managed node.

For copying we will use COPY module in ansible.

- name: Copying Content
dest: /var/www/html/udit.html
content: "Hello There"
notify: restart

Here we have used the content keyword this keyword will write the specified content in the mentioned file in the destination folder.

Now, we will write code for making our code expose to the world.

- name: Firewall Task
port: 80/tcp
state: enabled
immediate: yes

Now, it’s time for using handlers.

- name: restart
name: httpd
state: restarted

A section with tasks that are treated as handlers, won’t get executed normally, only when notified after each section of tasks is complete. A handler’s listen field is not template.

Full Code of Playbook:

After this, we will run the playbook.

To run the playbook:

ansible-playbook <File_name.yml>

Now, If we rerun the playbook our httpd service will not get restart because there is no change in the task named Copying Content as we have not changed anything.

See again after running the playbook our httpd service is not started again.

Now, let’s check the output of our playbook.

By writing our node IP address and file name we will get our output.

IP Address/File_name

Output of our Playbook.

💥 Thank you for reading. 💥



Uditanshu pandey

Technical Volunteer at ARTH-The School of Technologies || Cloud Enthusiast || Ansible ||Flutter || Hybrid Multi Cloud || DevOps || Terraform