{"id":296,"date":"2024-12-29T17:27:52","date_gmt":"2024-12-29T16:27:52","guid":{"rendered":"https:\/\/deleforterie.com\/wordpress\/?p=296"},"modified":"2024-12-29T17:27:55","modified_gmt":"2024-12-29T16:27:55","slug":"running-a-role-handler-from-a-playbook","status":"publish","type":"post","link":"https:\/\/deleforterie.com\/wordpress\/index.php\/2024\/12\/29\/running-a-role-handler-from-a-playbook\/","title":{"rendered":"Running a Role handler from a playbook"},"content":{"rendered":"\n<p>If you are well organized you have multiple roles to manage you deployment and configuration<\/p>\n<p>If you know the Ansible handler power you use it to manage all the operations on your software, services and so on.<\/p>\n<p>It is simple to trigger a handler inside a role task, but have you ever imagined using the role handlers from outside your role tasks, from a playbook ?<\/p>\n<p><!--more--><\/p>\n<p>If you can trigger immediately a handler from a playbook it opens multiple way of managing your software and services.<\/p>\n<p>Usually you have handlers for your software or services to<\/p>\n<ul>\n<li>Stop<\/li>\n<li>Start<\/li>\n<li>Restart<\/li>\n<li>Reload<\/li>\n<\/ul>\n<p>You use it inside role task to trigger a restart after a configuration change.<\/p>\n<p>Here is the simple example from Ansible documentation<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"YAML\">---\n- name: Ensure apache is at the latest version\n  ansible.builtin.yum:\n    name: httpd\n    state: latest\n\n- name: Write the apache config file\n  ansible.builtin.template:\n    src: \/srv\/httpd.j2\n    dest: \/etc\/httpd.conf\n  notify:\n    - Restart apache\n\n- name: Ensure apache is running\n  ansible.builtin.service:\n    name: httpd\n    state: started\n\nhandlers:\n  - name: Restart apache\n    ansible.builtin.service:\n      name: httpd\n      state: restarted<\/pre>\n<p>\u00a0<\/p>\n<p>Here inside a role we see that using <strong>notify<\/strong> associated to <strong>&#8220;Restart apache&#8221; <\/strong>will trigger the handler to restart service httpd<\/p>\n<p>If I want to call the handler from outside the role from a playbook because I have a succession of the role tasks that needs a restart of httpd service and I don&#8217;t want to restart it at each task call from the playbook but only at the last one.<\/p>\n<p>Here how to proceed :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"YAML\">- name: Multiple apache tasks\n  hosts: webservers\n  tasks:\n\n  - name: Write apache config file\n    ansible.builtin.include_role:\n      name: ansible-role-apache\n      tasks_from: \"{{ task }}\"\n    loop:\n      - \"config\/app1\"\n      - \"config\/app2\"\n      - \"config\/app3\"\n      - \"config\/app4\"\n      - \"config\/app5\"\n    loop_control:\n      loop_var: task\n\n  - name: Write apache files\n    ansible.builtin.include_role:\n      name: ansible-role-apache\n      tasks_from: \"{{ task }}\"\n    loop:\n      - \"files\/app1\"\n      - \"files\/app2\"\n      - \"files\/app3\"\n      - \"files\/app4\"\n      - \"files\/app5\"\n    loop_control:\n      loop_var: task\n\n- name: Restart apache\n  hosts: webservers\n  tasks:\n\n  - name: Include role only without any task running to have access to handlers\n    ansible.builtin.include_role:\n      name: ansible-role-apache\n\n  - name: Calling role handlers\n    ansible.builtin.debug:\n      msg: \"Restarting apache after multiple application configuration\/files changes\"\n    notify: \"ansible-role-apache: Restart apache\"\n    changed_when: true\n<\/pre>\n<p>\u00a0<\/p>\n<p>This will call the handler from the playbook, very useful to organized your code and manage all software or services operations.<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are well organized you have multiple roles to manage you deployment and configuration If you know the Ansible handler power you use it to manage all the operations on your software, services and so on. It is simple to trigger a handler inside a role task, but have you ever imagined using the&hellip;<\/p>\n","protected":false},"author":2,"featured_media":155,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[41,43,42],"class_list":["post-296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible","tag-handler","tag-notify","tag-playbook"],"_links":{"self":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":2,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":298,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/296\/revisions\/298"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media\/155"}],"wp:attachment":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}