đź”° 11.3 Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a way to rectify this challenge in Ansible playbook

First try to restart the services :

Ansible playbook to restart the webserver :

Now build this ansible playbook:-

command: ansible-playbook file_name.yml

Now if I again restart the service then one challenge will occur that it will again restart the service but previously only we started the service. It is not means that we did mistake some where it means the restart service is not idempotence in nature…..

Now first understand what the idempotence means ??

Idempotence:- It is the property of certain operations in mathematics and computer science that can be applied multiple times without changing the result beyond the initial application.

Same here without changing anything in the code it will restart the service again and again which required more resources which is good in our testing case but in prospective of industry it is very bad……

So to make this service Idempotence we need to introduced a new concept i.e notify and handlers.

Notify:- It is just like a trigger which can only trigger the handlers when it detect or notice that some part of the code is changed or removed in the file or folder.

Handlers:- Handlers are just like regular tasks in an Ansible playbook, but are only run if the Task contains a notify directive and also indicates that it changed something.

“ For example, if a config file is changed then the task referencing the config file templating operation may notify a service restart handler “

No apply this concept and make the service Idempotence in nature :-

Ansible playbook to restart the service using handlers and notify :

Build the Ansible file :-

command: ansible-playbook file_name.yml

Now we can see that we didn’t do any changes in the file so that the restart service won’t run and we want that only…………

Here is the output !!!!

Thanks for reading !!!