Before moving to the topic, let's familiar with one unique unfamiliar concept/vocabulary ie Idempotence. Actually, this concept is borrowed from Mathematics.
Hope all are familiar with Zero. If any number multiply with zero in "N" number of times, its output will be throughout the same. This concept is called Idempotence.
Any operations are said to be Idempotent if the result of performing once is exactly equal to the result of performing multiple times.
In other words, An Idempotent operation is one that can be applied multiple times without changing the result beyond the initial application.
Ok, let's take one real industry use-case so it will make more sense to you.
It is assumed that you are familiar with the HTTPD web server which is used for the deployment of your code to your web-client.
We all know that HTTPD server's default port number is 80 and its default document root is /var/www/html. HTTPD server can only deploy the files present in Document root.
But we can change all these settings by entering inside the HTTPD configuration file, but after make changes we have to restart the HTTPD so that only the changes made by you will update or else it wont.
We already know how to launch the webserver in Docker container using Ansible. If not, please refer to my previous blog by clicking here
Here we are going to launch the web server in Managed node's EC2 instance.
Challenges
If you run the following scripts to start the webserver,
if there will be any change in configuration file like changing port number or changing document root, the change won't get updated while running playbook for next time.
It can be solved by replacing started with restarted, but it will cause other problems, the problem is every time if you run the playbook, it will always restart because it has no intelligence to check whether there is a change or not and it also consumes more resources.
For us, it should not run everytime, it should run only once when the property of its configuration file changes.
For solving this challenge, We are going to use Ansible File handlers. It can run only once when it get notified.
Code with Explanation
Here, the Installation of the HTTPD web server is done using yum module and Copying the files to its document root by using copy module.
So far, it is simple only.
Then We are going to change the default port number of httpd server in Managed node's configuration file using Regular Expression (ReGex).
And we are also using Notify keyword, it will notify/trigger restart operation.
Note: Notify will only notify to the Restart handler when there is any change of Replacement operation, or else it won't notify and sends the trigger to restart HTTPD.
If there will be no change, then "Start HTTPD" will run and it simply start the service.
In this way, We can save the CPU & RAM resources whenever there will be a need, then only HTTPD will restart, it won't run every time when playbooks are executed.
You can find these YAML scripts through my Github.
Github link: https://github.com/vishnuswmech/Providing-Idempotence-property-to-HTTPD-Service-restart-using-Ansible-handlers.git
If you have queries, You can also connect with me though
Top comments (0)