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
content: "idempotence retained...!!"
- name: "start the service"
port : 80/tcp
state : enabled
- name: "restarting httpd service"
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 ⬇
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build…
Thanks For Reading the Article !!