Fix defenition of multiple static routes for network

Current logic was relying on iteration inside the template. However,
since config_template module was used to deliver network configuration
it was merging sections having same name together.

While this behaviour is correct one for config_template as all sections
must be unique from ConfigParser perspective and in order to apply
overrides properly, it was not suiting the way how routes should be
defined in networkd configuration.

To workaround the issue we place routes separately under <network>.d
directory, which should be supported by systemd [1]

[1] https://www.freedesktop.org/software/systemd/man/latest/systemd.network.html

Closes-Bug: #2045819
Change-Id: I01aa44dcdc85e32d18dd52bcd4878a9017fb6ead
This commit is contained in:
Dmitriy Rabotyagov 2023-12-08 18:47:35 +01:00 committed by Dmitriy Rabotyagov
parent 18b36137dc
commit 70442c5efb
4 changed files with 38 additions and 8 deletions

View File

@ -0,0 +1,11 @@
---
fixes:
- |
Multiple routes can be supplied to the systemd network and they will be
placed to a separate configuration file
`/etc/systemd/network/{{ filename }}.d/routes.conf`
Previously defining multiple routes will result in squashing them together
under same section name, while for them to work properly each descriped
route must be placed in it's own section.

View File

@ -184,6 +184,24 @@
tags:
- systemd-networkd
- name: Create systemd-networkd extra config folder
ansible.builtin.file:
path: "/etc/systemd/network/{{ item }}.network.d"
owner: "root"
group: "root"
mode: "0755"
state: directory
loop: "{{ _systemd_networks_named | selectattr('static_routes', 'defined') | map(attribute='filename') }}"
- name: Place systemd-networkd network routes
ansible.builtin.template:
src: systemd-network-routes.j2
dest: "/etc/systemd/network/{{ item['filename'] }}.network.d/routes.conf"
owner: "root"
group: "root"
mode: "0644"
loop: "{{ _systemd_networks_named | selectattr('static_routes', 'defined') }}"
- name: Enable and start systemd-networkd
systemd:
name: "systemd-networkd"

View File

@ -0,0 +1,9 @@
# {{ ansible_managed }}
{% for route in item['static_routes'] %}
[Route]
Destination={{ route['cidr'] }}
Gateway={{ route['gateway'] }}
Metric={{ 20 + loop.index }}
{% endfor %}

View File

@ -10,14 +10,6 @@ UseNTP=yes
RouteMetric=20
{% endif %}
{% for route in item.1.static_routes | default([]) %}
[Route]
Destination={{ route['cidr'] }}
Gateway={{ route['gateway'] }}
Metric={{ 20 + loop.index }}
{% endfor %}
{%- if item.1.address is not defined %}
{# Address is not defined #}
{% set _addresses = [] %}