{"id":232,"date":"2022-05-16T12:41:38","date_gmt":"2022-05-16T12:41:38","guid":{"rendered":"http:\/\/checkmateq.com\/blog\/?p=232"},"modified":"2023-08-07T17:34:03","modified_gmt":"2023-08-07T17:34:03","slug":"ansible-handlers","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/ansible-handlers","title":{"rendered":"How to use Ansible Handlers in Playbook"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-246\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-300x200.jpg\" alt=\"\" width=\"737\" height=\"491\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-300x200.jpg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-1024x683.jpg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-768x512.jpg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-1536x1024.jpg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-2048x1365.jpg 2048w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/pexels-realtoughcandycom-11035393-1200x800.jpg 1200w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><a href=\"https:\/\/www.checkmateq.com\/python-development\">Ansible<\/a> Handlers are tasks that are run when they are called by another task using <strong>Notify<\/strong>. It is useful when you want to run a task in response to some changes.<\/p>\n<p><strong>Handlers<\/strong> are defined globally using the <strong>handlers<\/strong> keyword. The tasks that need to call handlers have to define it with <strong>notify<\/strong> keyword. If no task call handlers then they will not run.<\/p>\n<p>The use-case of the handlers can be understood with an example.<\/p>\n<p>In the following playbook, we are installing httpd service and copying the configuration file from our machine to the remote node. Now if the configuration file is copied we want to restart the httpd service. We have defined it using <strong>notify<\/strong> keyword. We have defined the restart httpd task with the <strong>handlers<\/strong> keyword.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-233\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-114541-300x178.png\" alt=\"\" width=\"624\" height=\"370\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-114541-300x178.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-114541-768x456.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-114541.png 895w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><span class=\"l l-Scalar l-Scalar-Plain\"><strong>Grouping of handlers: <\/strong><\/span>A single task can be used to notify <strong>multiple<\/strong> handlers. In the following playbook, we are notifying two tasks together.<\/p>\n<p>One thing to note here is that handlers run in the order in which they are defined, not in the order in which they are written in notify statement.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-241\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-143151-300x216.png\" alt=\"\" width=\"538\" height=\"387\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-143151-300x216.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-143151.png 742w\" sizes=\"(max-width: 538px) 85vw, 538px\" \/><\/p>\n<p><strong>Changing the flow of handler execution:<\/strong> By default handlers run after, all the tasks in the playbook have been executed. If we want to change this flow and run the handler before the end of the play we can use the meta module.<\/p>\n<p><span class=\"p p-Indicator\"><img loading=\"lazy\" class=\"alignnone wp-image-260\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171322-300x242.png\" alt=\"\" width=\"540\" height=\"436\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171322-300x242.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171322-768x619.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171322.png 991w\" sizes=\"(max-width: 540px) 85vw, 540px\" \/><\/span><\/p>\n<p>At the time of running of the playbook, we can see that handler is running before the copy task.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-259\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171302-300x211.png\" alt=\"\" width=\"540\" height=\"380\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171302-300x211.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-171302.png 523w\" sizes=\"(max-width: 540px) 85vw, 540px\" \/><\/p>\n<p><strong><a href=\"https:\/\/www.checkmateq.com\/\">Using variables with ansible handlers<\/a>: <\/strong>Placing variables in the handler name should be avoided as it could lead the complete play failure because handler names are templated in starting so sometimes ansible may not have a value available for handler name like the following playbook.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-263\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-174613-300x42.png\" alt=\"\" width=\"529\" height=\"74\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-174613-300x42.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-174613.png 754w\" sizes=\"(max-width: 529px) 85vw, 529px\" \/><\/p>\n<p>Instead, variables should be placed in the task parameters like in the following playbook.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-262\" src=\"http:\/\/checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-172545-300x44.png\" alt=\"\" width=\"532\" height=\"78\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-172545-300x44.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-172545-768x114.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/05\/Screenshot-2022-05-16-172545.png 979w\" sizes=\"(max-width: 532px) 85vw, 532px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Connect with our <a href=\"https:\/\/www.checkmateq.com\/devops-engineering\">DevOps<\/a> team to know about DevOps management strategies.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Ansible Handlers are tasks that are run when they are called by another task using Notify. It is useful when you want to run a task in response to some changes. Handlers are defined globally using the handlers keyword. The tasks that need to call handlers have to define it with notify keyword. If &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/ansible-handlers\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to use Ansible Handlers in Playbook&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,2,12,7,11,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/232"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":24,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":4355,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions\/4355"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}