From 70442c5efb34222d0e333422fc469608795c70e4 Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Fri, 8 Dec 2023 18:47:35 +0100 Subject: [PATCH] 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 .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 --- ...workd_multiple_routes-becb9e691f38987a.yaml | 11 +++++++++++ tasks/main.yml | 18 ++++++++++++++++++ templates/systemd-network-routes.j2 | 9 +++++++++ templates/systemd-network.j2 | 8 -------- 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/networkd_multiple_routes-becb9e691f38987a.yaml create mode 100644 templates/systemd-network-routes.j2 diff --git a/releasenotes/notes/networkd_multiple_routes-becb9e691f38987a.yaml b/releasenotes/notes/networkd_multiple_routes-becb9e691f38987a.yaml new file mode 100644 index 0000000..0cf26db --- /dev/null +++ b/releasenotes/notes/networkd_multiple_routes-becb9e691f38987a.yaml @@ -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. diff --git a/tasks/main.yml b/tasks/main.yml index ecf6ab6..a109255 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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" diff --git a/templates/systemd-network-routes.j2 b/templates/systemd-network-routes.j2 new file mode 100644 index 0000000..81f9e06 --- /dev/null +++ b/templates/systemd-network-routes.j2 @@ -0,0 +1,9 @@ +# {{ ansible_managed }} + +{% for route in item['static_routes'] %} +[Route] +Destination={{ route['cidr'] }} +Gateway={{ route['gateway'] }} +Metric={{ 20 + loop.index }} + +{% endfor %} diff --git a/templates/systemd-network.j2 b/templates/systemd-network.j2 index 8546043..75fca96 100644 --- a/templates/systemd-network.j2 +++ b/templates/systemd-network.j2 @@ -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 = [] %}