{"id":2158,"date":"2024-07-01T12:55:06","date_gmt":"2024-07-01T10:55:06","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=2158"},"modified":"2024-10-03T10:56:52","modified_gmt":"2024-10-03T08:56:52","slug":"understanding-the-debug-module-in-ansible-vs","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/understanding-the-debug-module-in-ansible-vs\/","title":{"rendered":"Understanding the\u00a0debug\u00a0Module in Ansible:\u00a0|\u00a0vs.\u00a0>"},"content":{"rendered":"\n<p>One of the key features in Ansible is the ability to output messages to help debug and understand your playbooks. This is often done using the&nbsp;<code>debug<\/code>&nbsp;module. However, how you format your debug messages can significantly impact readability and output. In this post, we\u2019ll explore the <strong>difference<\/strong> <strong>between<\/strong> <strong>using<\/strong> the&nbsp;<code><strong>|<\/strong><\/code>&nbsp;and&nbsp;<code><strong>&gt;<\/strong><\/code>&nbsp;<strong>operators<\/strong> in Ansible\u2019s&nbsp;<code>debug<\/code>&nbsp;module.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The&nbsp;<code>debug<\/code>&nbsp;Module<\/h4>\n\n\n\n<p>The\u00a0<em><strong>debug<\/strong><\/em>\u00a0<strong>module<\/strong> is easy to use and lets you <strong>print messages during playbook execution<\/strong>, which is helpful for troubleshooting or displaying information.<\/p>\n\n\n\n<p>Here\u2019s a basic example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Debug message example\n  hosts: localhost\n  tasks:\n    - name: Print a debug message\n      debug:\n        msg: \"Hello, world!\"<\/code><\/pre>\n\n\n\n<p>This will output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TASK &#91;Print a debug message] ***************************************************\nok: &#91;localhost] =&gt; {\n    \"msg\": \"Hello, world!\"\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using&nbsp;<code>|<\/code>&nbsp;for Literal Block Scalars<\/h4>\n\n\n\n<p>When you use the<strong>&nbsp;<code>|<\/code>&nbsp;operator<\/strong>, you indicate that the text should be treated as a literal block scalar. This means that newlines are preserved as they are.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Debug message with literal block scalar\n  hosts: localhost\n  tasks:\n    - name: Print a multi-line debug message\n      debug:\n        msg: |\n          Hello, world!\n          This is an example.<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TASK &#91;Print a multi-line debug message] ****************************************\nok: &#91;localhost] =&gt; {\n    \"msg\": \"Hello, world!\\nThis is an example.\\n\"\n}<\/code><\/pre>\n\n\n\n<p>As you can see, the <strong>newlines<\/strong> <strong>are<\/strong> <strong>preserved<\/strong> in the output, maintaining the exact formatting you used in your playbook.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Using&nbsp;<code>&gt;<\/code>&nbsp;for Folded Block Scalars<\/h4>\n\n\n\n<p>Conversely, the&nbsp;<strong><code>&gt;<\/code>&nbsp;operator<\/strong> is used for folded block scalars. This means that <strong>newlines<\/strong> <strong>are<\/strong> <strong>replaced<\/strong> <strong>with<\/strong> <strong>spaces<\/strong>, except for double newlines, which are converted to single newlines.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- name: Debug message with folded block scalar\n  hosts: localhost\n  tasks:\n    - name: Print a folded multi-line debug message\n      debug:\n        msg: &gt;\n          Hello, world!\n          This is an example.<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TASK &#91;Print a folded multi-line debug message] *********************************\nok: &#91;localhost] =&gt; {\n    \"msg\": \"Hello, world! This is an example.\\n\"\n}<\/code><\/pre>\n\n\n\n<p>In this case, the newlines within the text are replaced by spaces, resulting in a single line of output.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use Each Operator<\/h4>\n\n\n\n<ul>\n<li><strong>Use&nbsp;<code>|<\/code>&nbsp;when you want to maintain the exact formatting of your text<\/strong>, including newlines. This is useful for long messages or logs that need to be displayed in a specific format.<\/li>\n\n\n\n<li><strong>Use&nbsp;<code>&gt;<\/code>&nbsp;when you want a more compact representation<\/strong>, where newlines are replaced by spaces. This can be handy for simpler messages or when you want to ensure the output is concise.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>One of the key features in Ansible is the ability to output messages to help debug and understand your playbooks. &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/understanding-the-debug-module-in-ansible-vs\/\" class=\"more-link\">More <span class=\"screen-reader-text\">Understanding the\u00a0debug\u00a0Module in Ansible:\u00a0|\u00a0vs.\u00a0><\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1961,"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\/2158"}],"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=2158"}],"version-history":[{"count":2,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2158\/revisions"}],"predecessor-version":[{"id":2162,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2158\/revisions\/2162"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/1961"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=2158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=2158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=2158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}