Ansible with yaml synthaxe

chumi
Fri, 19 Sep 2025 18:18:22 +0000
Get Help

Hi,
i am trying to get something as:

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:

    - 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:

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.

14 posts - 6 participants