Using Handlers in Ansible
Yash Indane
Tech Enthusiast | Integrating Technologies | 1x AWS Certified | 6x Microsoft Certified | Cloud Computing | DevOps
ARTH - Task 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
Ansible follows idempotence with most of the tasks . But we some of the tasks it does not follow it. For example while restarting httpd service it does not follow idempotence. So for such cases we use Handlers.
What are Handlers?
Sometimes you want a task to run only when a change is made on a machine. For example, you may want to restart a service if a task updates the configuration of that service, but not if the configuration is unchanged. Ansible uses handlers to address this use case. Handlers are tasks that only run when notified. Each handler should have a globally unique name.
Let's see an example-
We have a controller node and a managed node . We will copy a file e.txt from controller to managed node. The file is not present in the managed node . If run this playbook then the file will be copied and so a change is made which will notify the handler to restart httpd.
But , now if run the playbook again , this time already the file is present in the specific folder of managed node , so it won't be copied due to idempotence nature of Ansible. Here no change is made so the handler is not notified , and hence httpd is not restarted.
PLAYBOOK-
Hence we can use Handlers to run tasks that have to be run only when notified , so they don't run every time and consume more resources.
Cloud Technical Solutions Engineer @ Google
4 年Great bro Yash Indane ??