This post talks about Ansible Handler,How to use Ansible Handlers?,ansible handlers notify,ansible handlers vs tasks,ansible handlers debug,ansible handlers global,ansible handlers example,ansible handler multiple tasks,ansible handler arguments,ansible add handler.
Instead of executing several tasks,we want some tasks(like-restart the servers)to be executed only when some event or status change occur.
--- -hosts:db user:ansadmin become:true become_methods:sudo tasks: -name:Install httpd package yum: name:"httpd" state:"installed" -name:starting the httpd service service: name:"httpd" state:"started" -name:Copying the index file copy: src:"/etc/ansible/index.html" dest:"/var/www/html/index.html" -name:Restarting the server service: name:"httpd" state:"restarted" …
The above playlist always restarts the server irrespective of the fact that any change happens or not.
To avoid this we can use handlers.
handlers: -name:Restarting the server services: name:"httpd" state:"restarted"
In task we need to include –
Notify: - Restarting the server
The handler name and the task level notify name should be same.Here notify name and task name should match and the comparison is case sensitive.
In this scenario,if a change in the task happens then handler will run.