# Whitespace in templates

**URL:** <https://forum.ansible.com/t/whitespace-in-templates/2173>\
**Category:** Get Help\
**Tags:** template, jinja2\
**Created:** [November 6, 2023, 4:34pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173 "2023-11-06T16:34:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://forum.ansible.com/u/devnull)\
**Post date:** [November 6, 2023, 4:34pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/1 "2023-11-06T16:34:49Z")

</div>

Hi,

it’s really a minor issue, but it’s at least annoying me in a way that I will ask for advice 🙂

I am creating a config based on this Jinja template:

```auto
    listen-on port 53 {
        {% for ip in ips -%}
        {{ ip }};
        {% endfor -%}
    };

```

**ips** is a list of IPs, like this:

```auto
ips:
  - 10.0.0.53
  - 10.0.2.53
  - 10.0.4.53

```

Now the result is as follows:

```auto
    listen-on port 53 {
        10.0.0.53;
        10.0.2.53;
        10.0.4.53;
        };

```

Based on the template I would expect the closing curly brace `};` to be positioned under the “l” of the “listen-on” statement, but clearly some whitespace has been added in front of it.

I have tried various options as described in [Template Designer Documentation — Jinja Documentation (3.1.x)](https://jinja.palletsprojects.com/en/3.1.x/templates/#whitespace-control) but I can’t get it to work the way I want it to output the config file.

What am I doing wrong ?

---

<div class="post-metadata">

**Author:** ![kpfleming](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@kpfleming](https://forum.ansible.com/u/kpfleming)\
**Post date:** [November 6, 2023, 5:21pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/2 "2023-11-06T17:21:08Z")

</div>

The four spaces before the `endfor` statement are being emitted into the output. I never indent Jinja2 control statements, for this exact reason.

---

<div class="post-metadata">

**Author:** ![tanganellilore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tanganellilore/32/178_2.png) [@tanganellilore](https://forum.ansible.com/u/tanganellilore)\
**Post date:** [November 6, 2023, 7:48pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/3 "2023-11-06T19:48:31Z")

</div>

Hi,  
As suggested not indent the jinja tag,like this

```auto
listen-on port 53 {
{% for ip in ips %}
    {{ ip }};
{% endfor %}
};

```

Anyway have you tried with this?

```auto
listen-on port 53 {
    {%- for ip in ips +%}
    {{ ip }};
    {%- endfor +%}
};

```

Should be work as same of non-indented jinja.

---

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://forum.ansible.com/u/devnull)\
**Post date:** [November 6, 2023, 8:12pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/4 "2023-11-06T20:12:23Z")

</div>

Right, that indeed did the trick. I wasn’t aware of this issue when indenting the control statements.

I removed it and now the output is as expected.

Thanks a lot !

---

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://forum.ansible.com/u/devnull)\
**Post date:** [November 6, 2023, 8:22pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/5 "2023-11-06T20:22:20Z")

</div>

Thanks, both of these produce the same output.

Regarding the second solution, did you get that from experience, or can you recommend a website (next to the Jinja homepage itself) where this is explained in more detail ?

---

<div class="post-metadata">

**Author:** ![tanganellilore](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/tanganellilore/32/178_2.png) [@tanganellilore](https://forum.ansible.com/u/tanganellilore)\
**Post date:** [November 6, 2023, 8:25pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/6 "2023-11-06T20:25:35Z")

</div>

Something from experience.  
Reading the documentation is not clear that we can combine thme, but it’s possible.

---

<div class="post-metadata">

**Author:** ![IPvSean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ipvsean/32/1153_2.png) [@IPvSean](https://forum.ansible.com/u/IPvSean)\
**Post date:** [November 6, 2023, 8:44pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/7 "2023-11-06T20:44:44Z")

</div>

Try this-\>

```auto
listen-on port 53 {
{% for ip in ips %}
    {{ ip }};
{% endfor -%}
};

```

The -% was correct… but the problem is that the actual endfor being pushed over to the right, caused the whole thing to space over for that final `};`

You can try this out with something like: [Ansible Template Tester](https://ansible.sivel.net/test/)

---

<div class="post-metadata">

**Author:** ![devnull](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@devnull](https://forum.ansible.com/u/devnull)\
**Post date:** [November 6, 2023, 8:50pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/8 "2023-11-06T20:50:11Z")

</div>

Thanks for the Template Tester link, I will certainly add that to my toolset !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 6, 2023, 8:50pm UTC](https://forum.ansible.com/t/whitespace-in-templates/2173/9 "2023-12-06T20:50:33Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
