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 = [] %}