{"id":1199,"date":"2024-03-22T08:20:28","date_gmt":"2024-03-22T08:20:28","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1199"},"modified":"2024-03-23T07:49:18","modified_gmt":"2024-03-23T07:49:18","slug":"ansible-blocks-and-error-handling","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1199","title":{"rendered":"Ansible &#8211; Blocks, Rescue and Error Handling"},"content":{"rendered":"<p>Usually the Handlers work with a single task. However the &#8220;<strong>blocks<\/strong>&#8221; feature in Ansible helps us to group multiple tasks under one <strong>block<\/strong> section and attach handlers or rescue feature to it.<\/p>\n<p>In the below example the Block and Rescue feature will be used to display a message if one of the task out of two fails. Please note that the\u00a0<strong>httpd<\/strong> service is\u00a0<strong>NOT<\/strong> installed. Therefore the ansible task to start the httpd service will\u00a0<strong>fail<\/strong>.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat block_n_rescus.yml<\/p>\n<p>==== ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Copy a file to managed node if the httpd server that is stopped in present state is started by the play book<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: 192.168.48.129<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: A Block of tasks<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>block:<\/strong><\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Restart httpd server<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.service:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: httpd<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: started<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enabled: false<\/em><\/p>\n<p><em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Copy Index file<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.copy:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; src: shiju.html<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dest: \/var\/www\/html\/index.html<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>rescue:<\/strong><\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Error messahe when error identified<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.debug:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg: &#8220;An error was seen on block&#8221;<\/em><\/p>\n<p>==== ===<\/p>\n<p>Let us execute the playbook and see if the message in\u00a0<strong>rescue<\/strong> section is displayed<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout block_n_rescus.yml<\/p>\n<p>======= ====<br \/>\n<em>PLAY [Copy a file to managed node if the httpd server that is stopped in present state is started by the play book] ***<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><\/p>\n<p><em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Restart httpd server] ****************************************************<\/em><br \/>\n<em>fatal: [192.168.48.129]: FAILED! =&gt; {&#8220;changed&#8221;: false, &#8220;msg&#8221;: &#8220;Could not find the requested service httpd: host&#8221;}<\/em><\/p>\n<p><em>TASK [Error messahe when error identified] *************************************<\/em><br \/>\n<em>ok: [192.168.48.129] =&gt; {<\/em><br \/>\n<em> <span style=\"color: #ff0000;\">&#8220;msg&#8221;: &#8220;An error was seen on block&#8221;<\/span><\/em><br \/>\n<em>}<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0<\/em><br \/>\n==== ====<\/p>\n<p>Now let us <em>install the httpd service<\/em> and then re-execute the playbook. As the service is present, all tasks should get executed without any error, and therefore the\u00a0<strong>rescue<\/strong> section should not get called.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ssh 192.168.48.129 &#8216;dnf install httpd -y&#8217;<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout block_n_rescus.yml<\/p>\n<p>======= ===<\/p>\n<p><em>PLAY [Copy a file to managed node if the httpd server that is stopped in present state is started by the play book] ***<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Restart httpd server] ****************************************************<\/em><br \/>\n<em>changed: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Copy Index file] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0<\/em><br \/>\n=== ==== ===<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Usually the Handlers work with a single task. However the &#8220;blocks&#8221; feature in Ansible helps us to group multiple tasks under one block section and <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1199\" title=\"Ansible &#8211; Blocks, Rescue and Error Handling\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,21,3],"tags":[],"class_list":["post-1199","post","type-post","status-publish","format-standard","hentry","category-ansible","category-devops","category-linux"],"_links":{"self":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1199"}],"version-history":[{"count":12,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1199\/revisions"}],"predecessor-version":[{"id":1220,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1199\/revisions\/1220"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1199"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}