Hello DevOps Experts!! let's zoom into the usage of the Ansible Handlers and notifies
What are Ansible Handlers?
The handlers section or the tasks defined under the handlers folder are executed at the end of the play once all tasks are finished. In the handlers tasks we are typically do either start, reload, restart and stop services.
Sometimes we may need to execute the task only when a particular change is made that can be notified. Simple example of Apache web server when we modify httpd.conf file then we want to restart the httpd service.
When we were working on Tomcat, when tomcat service is enabled. then there is a need for the reload firewalld service this is where we need to move this reload task under handlers and the enable tomcat service should have notify the task name 'reload firewalld service'. These are the perfect examples for handlers usage in Ansible play.
So here the point is that handler tasks will be performed only when they are notified.
Ensure that handler task name should have a globally unique name.
Let's see the example of notify and handlers
--- - name: install php yum: name=((item)) state=latest with_items: - php - php-gd - php-pear - php-mysql notify: restart httpd handler handler/main.yml - name: rstart apache service: name=httpd state=restartedNow you can see how to include the role php-webserver inot the main playbook
# Filename: test-handler.yml --- - hosts: web user: root become: yes roles: - php-webserverExecution of the test-handler will be like this:
ansible-playbook test-handler.yml
No comments:
Post a Comment