{"id":18030,"date":"2022-08-03T18:51:41","date_gmt":"2022-08-03T18:51:41","guid":{"rendered":"https:\/\/linuxways.net\/?p=18030"},"modified":"2022-08-17T12:16:23","modified_gmt":"2022-08-17T12:16:23","slug":"ansible-debug-module","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/ansible-debug-module\/","title":{"rendered":"Ansible \u201cDebug\u201d Module"},"content":{"rendered":"<p>Debugging process is a very critical part of developing a piece of software code. Ansible playbooks are not an exception for this.<\/p>\n<p>For printing variables or messages on the terminal output during execution, Ansible provides a module called \u201cdebug\u201d. It is a very beneficial utility for developing a playbook. For example, it can be used with the \u201cwhen:\u201d directive and during the execution without interrupting the playbook.<\/p>\n<h2><strong>What Will We Cover?<\/strong><\/h2>\n<p>In this tutorial, we will see about the \u201cdebug module\u201d in Ansible. We will also learn about the various use cases. Let&#8217;s get started now.<\/p>\n<p><strong>Prerequisites<\/strong><\/p>\n<p>To perform the examples shown in this tutorial, we need to have the following requirements:<\/p>\n<p>1. You should have an installed Ansible on the controller node (Ubuntu 20.04 in our case).<\/p>\n<p>2. You should have a basic knowledge about what the purpose of Ansible is and how to write a playbook (and of course you need to know what a playbook is).<\/p>\n<p>If you are just a beginner in the Ansible world, we suggest that you first learn about Vagrant and how to set up a basic local testing environment using Vagrant. Once you set up a basic lab of having one controller node and two target nodes, you are all set to perform the examples in this guide.<\/p>\n<h2><strong>Parameters Used with Debug Module<\/strong><\/h2>\n<p>There are several parameters that are used with this module:<\/p>\n<p>1. msg: It is a string type parameter that prints a custom message.<\/p>\n<p>2. var: It specifies the variable to be debugged: \u201cmsg\u201d and \u201cvar\u201d are both mutually exclusive.<\/p>\n<p>3. verbosity: This sets the number only after which the debug operation runs. For example, if it is set to 2, the debug runs only when \u201c-vv\u201d is specified.<\/p>\n<h2><strong>Example Uses of Debug Module<\/strong><\/h2>\n<h3><strong>1. Printing a Simple Statement<\/strong><\/h3>\n<p>In its most basic usage, the debug module can be used for printing a statement in the output. Create a playbook \u201cdebug-demo.yml\u201d with the following content:<\/p>\n<pre>\r\n---\r\n\r\n- name: Ansible debug module basic example\r\n\r\nhosts: web\r\n\r\ntasks:\r\n\r\n- name: Basic debug module message\r\n\r\ndebug:\r\n\r\nmsg: \"LinuxHint Ansible\"\r\n<\/pre>\n<p>To run this playbook and any subsequent playbook, use the following format:<\/p>\n<pre>\r\n$ ansible-playbook example.yml -i \/path\/to\/inventory\/file\r\n<\/pre>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"964\" height=\"477\" class=\"wp-image-18034\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-1.png 964w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-1-300x148.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-1-768x380.png 768w\" sizes=\"auto, (max-width: 964px) 100vw, 964px\" \/><\/em><\/p>\n<h3><strong>2. Printing a Variable<\/strong><\/h3>\n<p>Beside printing the basic statements on the terminal, we can also use the \u201cdebug\u201d module to print the values of the variables. In the following example, we set a variable in the \u201cmy_vars.yml\u201d file and output its value using the \u201cmsg\u201d parameter of the debug module:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: web\r\n\r\nvars_files:\r\n\r\n- demo_vars.yml\r\n\r\ntasks:\r\n\r\n# Display \"Variable \u2018demo_var1\u2019 is set to \u2018demo_val1\u2019\".\r\n\r\n- debug: msg=\"Variable \u2018demo_var1\u2019 is set to {{ demo_var1 }}\"\r\n<\/pre>\n<p>The previous playbook prints the value of the variable \u201cdemo_var1\u201d along with the message specified in the \u201cmsg\u201d parameter.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"955\" height=\"474\" class=\"wp-image-18039\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-2.png 955w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-2-300x149.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-2-768x381.png 768w\" sizes=\"auto, (max-width: 955px) 100vw, 955px\" \/><\/p>\n<p>Similarly, the variables declared inside a playbook can also be used for printing on the terminal during a playbook execution:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: web\r\n\r\nvars:\r\n\r\nmy_var2: my_val2\r\n\r\ntasks:\r\n\r\n# This task displays \"Variable \u2018my_var2\u2019 is set to \u2018my_val2\u2019\".\r\n\r\n- debug: msg=\"Variable \u2018my_var2\u2019 is set to {{ my_var2 }}\"\r\n<\/pre>\n<p>In the previous playbook, the variable \u201cmy_var2\u201d is printed during execution.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"966\" height=\"468\" class=\"wp-image-18046\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-3.png 966w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-3-300x145.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-3-768x372.png 768w\" sizes=\"auto, (max-width: 966px) 100vw, 966px\" \/><\/p>\n<h3><strong>3. Using Debug Module with the Register Variables<\/strong><\/h3>\n<p>In the similar approach as in the previous examples, we can also use the debug module with the Register variables. Now, let us create another playbook with the following content:<\/p>\n<pre>\r\n---\r\n\r\n- hosts: all\r\n\r\ngather_facts: no\r\n\r\nbecome: false\r\n\r\ntasks:\r\n\r\n- name: Check the user name\r\n\r\nansible.builtin.shell: \/usr\/bin\/whoami\r\n\r\nregister: login\r\n\r\n- name: Display the user name using the output from previous task\r\n\r\ndebug: msg=\"Logged in as user {{ login.stdout }}\"\r\n<\/pre>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"973\" height=\"502\" class=\"wp-image-18056\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-4.png 973w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-4-300x155.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-4-768x396.png 768w\" sizes=\"auto, (max-width: 973px) 100vw, 973px\" \/><\/em><\/p>\n<p>The previous playbook has two tasks. The first task gives the name of the user from which the shell command was run and stores this value in a register type variable \u201clogin\u201d. The second task uses this variable in the printing the message using the \u201cmsg\u201d parameter of the debug module.<\/p>\n<h3><strong>4. Using the Debug Module Along with the \u201cWhen\u201d Conditional <\/strong><\/h3>\n<p>In this case, we can set the condition for the debug module to run only when certain conditions are satisfied. Create a new playbook with the following contents:<\/p>\n<pre>\r\n&nbsp;\r\n\r\n---\r\n\r\n- hosts: all\r\n\r\ngather_facts: yes\r\n\r\nbecome: true\r\n\r\ntasks:\r\n\r\n- name: Checking the release information of the servers\r\n\r\nansible.builtin.command: \/usr\/bin\/lsb_release -a\r\n\r\nregister: info\r\n\r\n- name: Print the release information\r\n\r\ndebug: msg=\"The System information is as follows {{ info.stdout }}\"\r\n\r\nwhen: ansible_facts['distribution']==\"Debian\"\r\n<\/pre>\n<p><em><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"574\" class=\"wp-image-18062\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-5.png 960w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-5-300x179.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-5-768x459.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/08\/word-image-18030-5-501x300.png 501w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/em><\/p>\n<p>In the previous playbook, we used the \u201clsb_release \u2013a\u201d command to print the release information for a system. The \u201cwhen\u201d statement sets a condition to print this information using the \u201cdebug\u201d module only when the OS distribution is Debian.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>In this guide, we have seen how to use the \u201cdebug\u201d module with some examples. The Ansible \u201cdebug\u201d module is very useful for actively debugging operations as we seen in the given examples. It is equally helpful for getting a verbose output from a playbook.<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>Practical tutorial about the Ansible debug module and its various cases for printing variables or messages on the terminal output during execution.<\/p>","protected":false},"author":102,"featured_media":18098,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-18030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=18030"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18030\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18098"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=18030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=18030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=18030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}