Concept of Handlers in Ansible
🔰 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
CHALLENGE
When we use Ansible playbook to deploy a httpd server in a system , we have give argument "restarted" in the service module i.e. the service will be restarted every time we run the playbook . Restarting the server again and again will make the server unavailable for that time . We want that sever gets restarted only when something is changed in the configuration file .
SOLUTION
We can overcome this issue using handlers . A task under handler keyword runs only when it is notified by some other task . We can put a "notify" keyword in task which upload configuration file to managed node , this will notify the given task if there is any change in the configuration file upload task .
IMPLIMENTING THIS SOLUTION
Here I created a playbook to change the document root of httpd server
my inventory file -
my playbook -
On running this playbook using command
# ansible-playbook <playbook name>
This is the result when we don't change anything in configuration file -
And this is the one when we change the document route in configuration file -
Webserver form browser -
Hence we have successfully given idempotence nature to restarting of webserver .
thank for reading....................