# Ansible with yaml synthaxe

**URL:** <https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519>\
**Category:** Get Help\
**Tags:** ubuntu\
**Created:** [September 19, 2025, 6:18pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519 "2025-09-19T18:18:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![chumi](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@chumi](https://forum.ansible.com/u/chumi)\
**Post date:** [September 19, 2025, 6:18pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/1 "2025-09-19T18:18:22Z")

</div>

Hi,  
i am trying to get something as:

```auto
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses: [x.x.x.1/24]
      routes:
        - to: 10.1.1.0/24
          via: x.x.x.254
        - to: 10.1.2.0/24
          via: x.x.x.254

```

Here is my code:

```auto
    - name: network | ubuntu | configure | update {{ etc_netplan_adm }} file
      ansible.builtin.copy:
        dest: "{{ etc_netplan_adm }}"
        content: |
          {{ ansible_managed_message }}
          network:
            version: 2
            ethernets:
              {{ iface_adm }}:
                dhcp4: no
                dhcp6: no
                addresses: [{{ addr_adm[0].address }}/{{ addr_adm[0].cidr }}]
                routes:
                  {% for route in adm_default_routes | flatten | map('string') | list %}
                  - to: {{ route }}
                    via: {{ addr_adm[0].gateway }}
                {% endfor %}

```

But with this code, i get something that is not matching to yaml format:

```auto
network:
  version: 2
  ethernets:
    ens32:
      dhcp4: no
      dhcp6: no
      addresses: [x.x.x.232/24]
      routes:
              - to: 10.1.1.0/24
          via: x.x.x.254
              - to: 10.1.2.0/24
          via: x.x.x.254

```

So it fails when running ‘netplan apply’.

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![jrglynn2](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@jrglynn2](https://forum.ansible.com/u/jrglynn2)\
**Post date:** [September 19, 2025, 7:02pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/2 "2025-09-19T19:02:33Z")

</div>

Sorry, not fully understanding … is it just the indentation of the `- to:` lines that `is not matching to yaml format`?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 19, 2025, 7:08pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/3 "2025-09-19T19:08:34Z")

</div>

```yaml
    - name: network | ubuntu | configure | update {{ etc_netplan_adm }} file
      ansible.builtin.copy:
        dest: "{{ etc_netplan_adm }}"
        content: |
          {{ ansible_managed_message }}
          network:
            version: 2
            ethernets:
              {{ iface_adm }}:
                dhcp4: no
                dhcp6: no
                addresses: [{{ addr_adm[0].address }}/{{ addr_adm[0].cidr }}]
                routes:
          {% for route in adm_default_routes | flatten | map('string') | list %}
                  - to: {{ route }}
                    via: {{ addr_adm[0].gateway }}
          {% endfor %}
```

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [September 19, 2025, 7:25pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/4 "2025-09-19T19:25:55Z")

</div>

I was able to get this to work doing by adjusting the spacing. It looks awful, but it seems to produce the correct results.

```yml
---
- name: Write File
  hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: "network | ubuntu | configure | update {{ etc_netplan_adm }} file"
      ansible.builtin.copy:
        dest: "{{ etc_netplan_adm }}"
        content: |
          {{ ansible_managed_message }}
          network:
            version: 2
            ethernets:
              {{ iface_adm }}:
                dhcp4: no
                dhcp6: no
                addresses: [{{ addr_adm[0].address }}/{{ addr_adm[0].cidr }}]
                routes:
                {% for route in adm_default_routes | flatten | map('string') | list %}
            - to: {{ route }}
                    via: {{ addr_adm[0].gateway }}
                {% endfor %}
      vars:
        iface_adm: eth0
        addr_adm:
          - address: 10.1.1.1
            cidr: 24
            gateway: x.x.x.254
        adm_default_routes:
          - 10.1.1.0/24
          - 10.1.2.0/24
          - 10.1.3.0/24
        etc_netplan_adm: ./netplan.yml
        ansible_managed_message: "## Block Managed via Ansbile"

```

I was also able to get this to work using `ansible.builtin.template`

`templates/netplan.yml.j2`

```auto
{{ ansible_managed_message }}
network:
  version: 2
  ethernets:
    {{ iface_adm }}:
      dhcp4: no
      dhcp6: no
      addresses: [{{ addr_adm[0].address }}/{{ addr_adm[0].cidr }}]
      routes:
      {% for route in adm_default_routes | flatten | map('string') | list %}
  - to: {{ route }}
          via: {{ addr_adm[0].gateway }}
      {% endfor %}

```

and a playbook `template_test.yml`

```yml
---
- name: Write File - Template
  hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: "network | ubuntu | configure | update {{ etc_netplan_adm }} file"
      ansible.builtin.template:
        src: "{{ template_file }}"
        dest: "{{ etc_netplan_adm }}"
      vars:
        iface_adm: eth0
        addr_adm:
          - address: 10.1.1.1
            cidr: 24
            gateway: x.x.x.254
        adm_default_routes:
          - 10.1.1.0/24
          - 10.1.2.0/24
          - 10.1.3.0/24
        etc_netplan_adm: ./netplan.yml
        ansible_managed_message: "## Block Managed via Ansbile"
        template_file: netplan.yml.j2

```

Neither of them have spacing that looks reasonable, but the both seem to produce a file that _looks_ like it matches your desired state.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Narizz28](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/narizz28/32/9476_2.png) [@Narizz28](https://forum.ansible.com/u/Narizz28)\
**Post date:** [September 19, 2025, 7:40pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/5 "2025-09-19T19:40:53Z")

</div>

Turn off Jinja2 trim\_blocks by putting a plus symbol at the end of the block:

{% endfor **+** %}

Everything in the block will start at the same indentation depth as the block itself:

```auto
routes:
  {% for route in adm_default_routes | flatten | map('string') | list %}
  - to: {{ route }}
    via: {{ addr_adm[0].gateway }}
  {% endfor +%}

```

EDIT: I Put too many spaces in the for loop. corrected

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [September 19, 2025, 7:46pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/6 "2025-09-19T19:46:45Z")

</div>

Using the code I provided above (adjusting to match your suggestions) this didn’t work as expected. I ended up with results mangled in the same way as the initial posting.

Is there a setting in `ansible.cfg` (or elsewhere) that might need enabled for this too? If it work working as described, I much prefer your solution to the much harder to read “working” examples I provided.

---

<div class="post-metadata">

**Author:** ![Narizz28](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/narizz28/32/9476_2.png) [@Narizz28](https://forum.ansible.com/u/Narizz28)\
**Post date:** [September 19, 2025, 8:07pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/7 "2025-09-19T20:07:17Z")

</div>

Try put the Plus at the end of the block opening statement instead:  
{% for route in adm\_default\_routes | flatten | map(‘string’) | list **+** %}

```auto
routes:
  {% for route in adm_default_routes | flatten | map('string') | list +%}
  - to: {{ route }}
    via: {{ addr_adm[0].gateway }}
  {% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [September 19, 2025, 8:13pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/8 "2025-09-19T20:13:28Z")

</div>

That’s much closer. The only thing that I’d consider still sub-optimal is additional newlines. The relevant block looks like this in both the inline and template file versions

```yml
      routes:
      
        - to: 10.1.1.0/24
          via: x.x.x.254
      
        - to: 10.1.2.0/24
          via: x.x.x.254
      
        - to: 10.1.3.0/24
          via: x.x.x.254

```

---

<div class="post-metadata">

**Author:** ![Narizz28](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/narizz28/32/9476_2.png) [@Narizz28](https://forum.ansible.com/u/Narizz28)\
**Post date:** [September 19, 2025, 8:31pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/9 "2025-09-19T20:31:42Z")

</div>

Kill the new lines with a minus sign in the block opening statement:  
{% **-** for route in adm\_default\_routes | flatten | map(‘string’) | list +%}

May also try on the block closing statement:  
{% **-** endfor %}

---

<div class="post-metadata">

**Author:** ![Lyle\_McKarns](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/lyle_mckarns/32/8142_2.png) [@Lyle\_McKarns](https://forum.ansible.com/u/Lyle_McKarns)\
**Post date:** [September 19, 2025, 8:37pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/10 "2025-09-19T20:37:29Z")

</div>

For both the inline version and the template version, I found that adding the `-` in both the beginning and block statements gave the desired results.

@chumi, this is what I ended up for the jinja block (either inline with `content` like you have, or in a separate template file) with after the help form @Narizz28

```yml
{{ ansible_managed_message }}
network:
  version: 2
  ethernets:
    {{ iface_adm }}:
      dhcp4: no
      dhcp6: no
      addresses: [{{ addr_adm[0].address }}/{{ addr_adm[0].cidr }}]
      routes:
      {%- for route in adm_default_routes | flatten | map('string') | list +%}
        - to: {{ route }}
          via: {{ addr_adm[0].gateway }}
      {%- endfor %}

```

---

<div class="post-metadata">

**Author:** ![Narizz28](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/narizz28/32/9476_2.png) [@Narizz28](https://forum.ansible.com/u/Narizz28)\
**Post date:** [September 19, 2025, 8:40pm UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/11 "2025-09-19T20:40:21Z")

</div>

For my preference, I like to put the loop block at the same indentation level as the outputs instead of the parent. It is easier to read to my eyes:

```auto
{{ ansible_managed_message }}
network:
  version: 2
  ethernets:
    {{ iface_adm }}:
      dhcp4: no
      dhcp6: no
      addresses: [{{ addr_adm[0].address }}/{{ addr_adm[0].cidr }}]
      routes:
        {%- for route in adm_default_routes | flatten | map('string') | list +%}
        - to: {{ route }}
          via: {{ addr_adm[0].gateway }}
        {%- endfor %}

```

---

<div class="post-metadata">

**Author:** ![chumi](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@chumi](https://forum.ansible.com/u/chumi)\
**Post date:** [September 22, 2025, 8:35am UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/12 "2025-09-22T08:35:24Z")

</div>

+1.  
I will remember that.

---

<div class="post-metadata">

**Author:** ![chumi](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@chumi](https://forum.ansible.com/u/chumi)\
**Post date:** [September 22, 2025, 8:37am UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/13 "2025-09-22T08:37:56Z")

</div>

+1.  
I like it because it is easier to read.

And many thanks for all your return.

---

<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:** [October 22, 2025, 8:38am UTC](https://forum.ansible.com/t/ansible-with-yaml-synthaxe/44519/14 "2025-10-22T08:38:19Z")

</div>

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