{"id":1191,"date":"2024-03-12T13:09:47","date_gmt":"2024-03-12T13:09:47","guid":{"rendered":"http:\/\/shijuvarghese.com\/?p=1191"},"modified":"2024-03-22T08:30:03","modified_gmt":"2024-03-22T08:30:03","slug":"ansible-error-handling","status":"publish","type":"post","link":"http:\/\/shijuvarghese.com\/?p=1191","title":{"rendered":"Ansible \u2013 Error Handling"},"content":{"rendered":"<p>Do not misunderstand the concept error handling in this document with error handling in usual software programming terminology.<\/p>\n<p>Usually when one writes a program, a syntax check will check for syntax including\u00a0indentation, spacing, etc. But it will not check if content such as package name to be installed using the playbook, parameters of a function in modules mentioned in the task , etc are correct. In this case the syntax check may be successful, but when running the playbook the execution may fail <strong>at<\/strong> the line incorrect parameters are mentioned and thus <em>halt further execution<\/em> of the playbook.<\/p>\n<p>The below example is of a playbook that tries installation of two dnf packages. However due to the typo related\u00a0httpd the remaining execution of the playbook to install mariadb server too stops.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat errorHandler.yml<\/p>\n<p>===== ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n&#8211; name: Task Failure Exercise<br \/>\n&nbsp; &nbsp; hosts: 192.168.48.129<br \/>\n&nbsp; &nbsp; vars:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; web_package: http<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; db_pacakge: mariadb-server<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; myPackage:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; http<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; mariadb-server<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; db_service: mariadb<br \/>\n&nbsp; &nbsp; tasks:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Install httpd<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.dnf:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ web_package }}&#8221;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Install mariadb<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.dnf:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ db_pacakge }}&#8221;<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<\/p>\n<p>==== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout errorHandler.yml &#8211;syntax-check<\/p>\n<p>===== ===<br \/>\n<em>playbook: \/root\/errorHandler.yml<\/em><\/p>\n<p>===== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout errorHandler.yml<\/p>\n<p>===== ====<\/p>\n<p><em>PLAY [Task Failure Exercise] ***************************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Install httpd] ***********************************************************<\/em><br \/>\n<em>fatal: [192.168.48.129]: FAILED! =&gt; {&#8220;changed&#8221;: false, &#8220;failures&#8221;: [&#8220;No package http available.&#8221;], &#8220;msg&#8221;: &#8220;Failed to install some of the specified packages&#8221;, &#8220;rc&#8221;: 1, &#8220;results&#8221;: []}<\/em><\/p>\n<p><em>PLAY RECAP *********************************************************************<\/em><br \/>\n<em>192.168.48.129 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0<\/em><br \/>\n<em>Please review the log for errors.<\/em><\/p>\n<p>===== ===<\/p>\n<p>Now let us add the error handling parameter and check the result.<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> cat errorHandler.yml<\/p>\n<p>==== ====<br \/>\n<em>&#8211; &#8211; &#8211;<\/em><br \/>\n<em>&#8211; name: Task Failure Exercise<\/em><br \/>\n<em> &nbsp; &nbsp; hosts: 192.168.48.129<\/em><br \/>\n<em> &nbsp; &nbsp; vars:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; web_package: http<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; db_pacakge: mariadb-server<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; myPackage:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; http<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#8211; mariadb-server<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; db_service: mariadb<\/em><br \/>\n<em> &nbsp; &nbsp; tasks:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Install httpd<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.dnf:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ web_package }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<\/em><br \/>\n<em> <span style=\"color: #ff0000;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ignore_errors: yes<\/span><\/em><\/p>\n<p><em> &nbsp; &nbsp; &nbsp; &nbsp; &#8211; name: Install mariadb<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ansible.builtin.dnf:<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: &#8220;{{ db_pacakge }}&#8221;<\/em><br \/>\n<em> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state: present<\/em><\/p>\n<p>==== ===<\/p>\n<p><strong>[root@centos9vm ~]#<\/strong> ansible-navigator run -m stdout errorHandler.yml<\/p>\n<p>===== ===<\/p>\n<p><em>PLAY [Task Failure Exercise] ***************************************************<\/em><\/p>\n<p><em>TASK [Gathering Facts] *********************************************************<\/em><br \/>\n<em>ok: [192.168.48.129]<\/em><\/p>\n<p><em>TASK [Install httpd] ***********************************************************<\/em><br \/>\n<em>fatal: [192.168.48.129]: FAILED! =&gt; {&#8220;changed&#8221;: false, &#8220;failures&#8221;: [&#8220;No package http available.&#8221;], &#8220;msg&#8221;: &#8220;Failed to install some of the specified packages&#8221;, &#8220;rc&#8221;: 1, &#8220;results&#8221;: []}<\/em><br \/>\n<span style=\"color: #ff0000;\"><em>&#8230;ignoring<\/em><\/span><\/p>\n<p><em>TASK [Install mariadb] *********************************************************<\/em><br \/>\n<em>changed: [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=1<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Do not misunderstand the concept error handling in this document with error handling in usual software programming terminology. Usually when one writes a program, a <a class=\"mh-excerpt-more\" href=\"http:\/\/shijuvarghese.com\/?p=1191\" title=\"Ansible \u2013 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-1191","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\/1191","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=1191"}],"version-history":[{"count":14,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1191\/revisions"}],"predecessor-version":[{"id":1208,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=\/wp\/v2\/posts\/1191\/revisions\/1208"}],"wp:attachment":[{"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1191"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/shijuvarghese.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}