How To Achieve Idempotence in Ansible While Restarting HTTPD Server!!

What is Idempotence ??

Idempotence means the action which gives no effect on performing the task when running it after the first time without changing nothing in it.

An Idempotent operation is one that can be applied multiple times without changing the result beyond the initial application, such as multiplication by zero. Idempotence is also very important to DevOps tools like Ansible which is the mostly using Automation tool ,because we can check configuration state and only make changes when necessary!!

What’s the Challange??

In any Webserver or In HTTPD webserver , the service has to be restarted every time a when any change is made in code or in any Configuration File So for this, usually we’ll give the keyword restarted under state ,But demerit of above this step is that even when no changes has been made in the configuration file the service will restart every time we run the playbook. But we want the service to be restarted only when there’s some changes in the webserver !!

How to solve this ❓🤔

The above idempontency problem can be solved by using Handlers task in Ansible Playbook,which are run only when notified.

What is Handlers??

Sometimes you want a task to run only when a change is made on a machine. 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 Move Towards the Practical Part ⬇:

Here,is the Playbook Code to Configure HTTPD Webserver.I have not used the Handlers task in it!!

- hosts: namenode
tasks:
- file:
state: directory
path: "/dvd"
- mount:
src: "/dev/cdrom"
path: "/dvd"
state: mounted
fstype: "iso9660"
- yum_repository:
baseurl: "/dvd/AppStream"
gpgcheck: no
name: "appstream"
description: "my_yum_appstream"
- yum_repository:
baseurl: "/dvd/BaseOS"
gpgcheck: no
name: "baseos"
description: "my_yum_baseos"
- package:
name: "httpd"
state: present
- copy:
content: "idempotence retained...!!"
dest: "/var/www/html/index.html"
- name: "start the service"
service:
name: "httpd"
state: started
- firewalld:
port : 80/tcp
state : enabled
permanent: no
immediate: yes
- name: "restarting httpd service"
service:
name: "httpd"
state: restarted

Running the Playbook for the 1st time:➡

Everything is fine till here .Now without changing anything in playbook code run the same playbook again➡

Here,while restarting the HTTPD service though we are running the playbook 2nd time with not changing anything at back-end still resources get engaged to restart it(Change Marked by Yellow-Colour).

Now,make use of Handlers in the Playbook as shown below⬇:

Run the Updated Playbook(This is 3rd time am Running the Playbook)➡

No change is shown in this playbook (by yellow-color) that means Idempotence is achieved while restarting the HTTPD service✨✨

For more Understanding visit the Git Hub Repository ⬇

Thanks For Reading the Article !!

--

--

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