{"id":60,"date":"2018-10-04T22:40:41","date_gmt":"2018-10-04T20:40:41","guid":{"rendered":"http:\/\/deleforterie.com\/wordpress\/?p=60"},"modified":"2023-01-18T08:53:13","modified_gmt":"2023-01-18T07:53:13","slug":"ansible-playing-with-jinja2-and-its-whitespace-control","status":"publish","type":"post","link":"https:\/\/deleforterie.com\/wordpress\/index.php\/2018\/10\/04\/ansible-playing-with-jinja2-and-its-whitespace-control\/","title":{"rendered":"Ansible &#8211; playing with Jinja2 and its whitespace control"},"content":{"rendered":"<p>Jinja2 templating with Ansible is powerfull and sometimes you could have some headache for positionning the result just where you want to respect a configuration file syntax.<\/p>\n<p>Following, I will show you how to manage the whitespace control with Jinja2<\/p>\n<p><!--more--><\/p>\n<p>Without specifying whitespace control<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {\n      {% if Var is defined %}\n      \"myParameter\": \"value1\",\n      {% else %}\n      \"myParameter\": \"value2\",\n      {% endif %}\n    }\n  }\n}<\/pre>\n<p>Result :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">{\n  \"Clusters\":{\n    \"properties\": {\n            \"myParameter\": \"value2\",\n          }\n  }\n}<\/pre>\n<p>As you can see the spaces of each lines will be keeped causing a gap<\/p>\n<p>With &#8211; everywhere<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {\n      {%- if Var is defined -%}\n      \"myParameter\": \"value1\",\n      {%- else -%}\n      \"myParameter\": \"value2\",\n      {%- endif -%}\n    }\n  }\n}<\/pre>\n<p>Result :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {\"myParameter\": \"value2\",}\n  }\n}<\/pre>\n<p>As you can see it will removed all the spaces and newlines<\/p>\n<p>With a &#8211; in front<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {\n      {%- if Var is defined %}\n      \"myParameter\": \"value1\",\n      {% else %}\n      \"myParameter\": \"value2\",\n      {% endif %}\n    }\n  }\n}<\/pre>\n<p>Result :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {      \"myParameter\": \"value2\",\n          }\n  }\n}<\/pre>\n<p>As you can see, the newline before the&nbsp;<code class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{%-<\/code>&nbsp; will be removed and the spaces in front of the line are concatenate at the end of the previous line<\/p>\n<p>With a &#8211; at the end of each block<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {\n      {% if Var is defined -%}\n      \"myParameter\": \"value1\",\n      {% else -%}\n      \"myParameter\": \"value2\",\n      {% endif -%}\n    }\n  }\n}<\/pre>\n<p>Result :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">{\n  \"Clusters\":{\n    \"properties\": {\n      \"myParameter\": \"value2\",\n      }\n  }\n}<\/pre>\n<p>As you can see this will removed all the spaces and newlines from the removed lines<\/p>\n<p>With a &#8211; at the beginning of each block<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{\n  \"Clusters\":{\n    \"properties\": {\n      {%- if Var is defined %}\n      \"myParameter\": \"value1\",\n      {%- else %}\n      \"myParameter\": \"value2\",\n      {%- endif %}\n    }\n  }\n}<\/pre>\n<p>Result :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"json\">{\n  \"Clusters\":{\n    \"properties\": {      \"myParameter\": \"value2\",    }\n  }\n}<\/pre>\n<p>As you can see the all the newlines will be removed and the spaces keeped<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jinja2 templating with Ansible is powerfull and sometimes you could have some headache for positionning the result just where you want to respect a configuration file syntax. Following, I will show you how to manage the whitespace control with Jinja2<\/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":[],"class_list":["post-60","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ansible"],"_links":{"self":[{"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/60","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=60"}],"version-history":[{"count":2,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":160,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/60\/revisions\/160"}],"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=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deleforterie.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}