making service restart Idempotent in Ansible

Deepak Kumar Pandia
4 min readMar 20, 2021

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code.

Ansible is one of the best tools in this automation world and ansible modules are the one who makes Ansible as best configuration tool.

most of the ansible modules are idempotent in nature but some of the module dont have the idempotent nature i.e. service module, so whenever we will use this module it will always run and consume our time and resources.

Now lets talk about how service module can be used in any playbook, so for this i am taking an example of httpd webserver

If we use httpd as a webserver and Service module to restart the webserver services than it will restart again and again even if there isn’t any changes in the config file of httpd.

Its not a good practice to restart the service again and again bcoz it might be possible that during the restarting the service some consumers visit our webserver and that time they will face downtime. From the business prespective it gives a very bad effect on the company’s reputation.

So it is necessary to have idempotent nature, so it will restart the service if there is any change in the conf file of httpd.

To make the module idempotent we have to ways :

Using Conditions (when keyword): we can use conditional statements for example, this task will run when given condition satisfies otherwise this task will skip.

using Handlers : handlers are more efficient and better choice to make the service module idempotent rather than using when keyword because handlers are meant for this kind of use-cases

To make the service module Idempotent we are going to use handlers:

For this i am using a AWS instance as managed node where i am going to install the webserver.

as you can see in this ansible-playbook i am using an EC2 instance with tag_Name_webserver to install the HTTPD webserver and than this playbook will copy our manually created config file from our local system to this EC2 instance.

This config file will assign one more port number 8080 for HTTPD and different DocumentRoot /var/www/test/ .

Now as you can see in the playbook i have used a notify keyword, this keyword will detect if there is any change in the task and if there is any change than it will inform the handler, than the handler will run.

now lets run this playbook and see what happens:

as you can see when i run this playbook first time than it will run all the tasks successfully, we can also check our webserver with 8080 port number is everything working fine or not.

as you can see webserver is working perfectly on the port number 8080, you can also check this with this command “#netstat -tnlp”

this command will show all the port numbers associated with the different processes. its clearly visible that httpd is associated with two different port numbers 80(default) and 8080.

now lets see if i run this ansible playbook again than what will happen

as you can see now handlers didn’t run because there isn’t any change in the conf file of httpd.

conclusion : we have successfully configured the idempotence nature or service module using handlers.

Thanks, Hope it’s Informative!!🙂

--

--