{"id":2088,"date":"2024-06-06T10:37:29","date_gmt":"2024-06-06T08:37:29","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=2088"},"modified":"2024-06-06T10:38:15","modified_gmt":"2024-06-06T08:38:15","slug":"how-to-force-handler-execution-in-ansible","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/how-to-force-handler-execution-in-ansible\/","title":{"rendered":"How to Force Handler Execution in Ansible"},"content":{"rendered":"\n<p>One of the key features of Ansible is the use of handlers, which are tasks that are executed only when notified by other tasks. However, sometimes we need these handlers to always run, regardless of whether they were notified. In this post, we&#8217;ll see how to achieve this using a real example with Nginx.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Handler in Ansible?<\/h2>\n\n\n\n<p>A handler in Ansible is a special task that runs when notified by other tasks. Handlers are useful for performing actions that should occur after one or more tasks have made changes to the system, such as restarting a service after updating its configuration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Forcing Handlers to Run<\/h2>\n\n\n\n<p>Normally, handlers only run if they are notified by a task. But what if we want to ensure a handler always runs? For this, we can use the <code><strong>meta:<\/strong> <strong>flush_handlers<\/strong><\/code> directive in our playbook.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Real Example: Restarting Nginx<\/h2>\n\n\n\n<p>Suppose we want to <strong>ensure<\/strong> that <strong>Nginx<\/strong> <strong>is restarted<\/strong> whenever we make changes to its configuration. Here\u2019s an example of how to structure a playbook to achieve this.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example Playbook<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>---\n- name: Ensure Nginx is always restarted\n  hosts: all\n  become: yes\n  tasks:\n    - name: Install Nginx\n      apt:\n        name: nginx\n        state: present\n      notify: restart nginx\n\n    - name: Create a simple index.html\n      copy:\n        content: \"&lt;html>&lt;body>&lt;h1>Hello, world!&lt;\/h1>&lt;\/body>&lt;\/html>\"\n        dest: \/var\/www\/html\/index.html\n      notify: restart nginx\n\n    - name: Force handlers to run\n      meta: flush_handlers\n\n    - name: Always restart Nginx\n      service:\n        name: nginx\n        state: restarted\n\n    - name: Another task to show it continues\n      debug:\n        msg: \"Nginx has been restarted, continuing with the playbook.\"\n\n  handlers:\n    - name: restart nginx\n      service:\n        name: nginx\n        state: restarted<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<ol>\n<li><strong>Install Nginx<\/strong>: This task installs Nginx and notifies the handler to restart Nginx.<\/li>\n\n\n\n<li><strong>Create a Simple index.html<\/strong>: This task creates or updates the <code>\/var\/www\/html\/index.html<\/code> file with simple HTML content and notifies the handler to restart Nginx.<\/li>\n\n\n\n<li><strong>Force Handlers to Run<\/strong>: This task ensures that any notified handlers up to this point are executed immediately.<\/li>\n\n\n\n<li><strong>Always Restart Nginx<\/strong>: This task ensures that Nginx is restarted regardless of whether it was notified or not.<\/li>\n\n\n\n<li><strong>Another Task<\/strong>: This is an example of a task that will run after the handlers have been flushed.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>With this configuration, we ensure that Nginx is always restarted whenever the playbook is run, ensuring that any changes to the configuration are properly applied. Using <code>meta: flush_handlers<\/code> and adding specific tasks to force handler execution gives us finer control over the behavior of our Ansible playbooks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the key features of Ansible is the use of handlers, which are tasks that are executed only when &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/how-to-force-handler-execution-in-ansible\/\" class=\"more-link\">More <span class=\"screen-reader-text\">How to Force Handler Execution in Ansible<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1806,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103,2],"tags":[106,112,126],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2088"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=2088"}],"version-history":[{"count":2,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2088\/revisions"}],"predecessor-version":[{"id":2091,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2088\/revisions\/2091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/1806"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}