Concept of Handlers in Ansible

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 -

No alt text provided for this image

my playbook -

No alt text provided for this image
No alt text provided for this image

On running this playbook using command

# ansible-playbook <playbook name>

This is the result when we don't change anything in configuration file -

No alt text provided for this image

And this is the one when we change the document route in configuration file -

No alt text provided for this image

Webserver form browser -

No alt text provided for this image

Hence we have successfully given idempotence nature to restarting of webserver .

thank for reading....................





To view or add a comment, sign in

More articles by Vivek Sharma

Insights from the community

Others also viewed

Explore topics