Dmitriy Rabotyagov 70442c5efb 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
2023-12-09 05:24:57 +00:00

64 lines
1.7 KiB
Django/Jinja

# {{ ansible_managed }}
[Match]
Name={{ item.1.interface }}
{% if item.1.address is defined and (item.1.address | lower) == 'dhcp' %}
[DHCP]
UseDNS={{ (item.1.usedns | default(false) | bool) | ternary('yes', 'no') }}
UseNTP=yes
RouteMetric=20
{% endif %}
{%- if item.1.address is not defined %}
{# Address is not defined #}
{% set _addresses = [] %}
{% elif item.1.address is iterable and item.1.address is not string %}
{# Address is a list, multiple addresses defined #}
{% set _addresses = item.1.address %}
{% else %}
{# Single address defined #}
{% set _addresses = [item.1] %}
{% endif %}
[Network]
{% for _addr in _addresses %}
{% if _addr is string %}
{% set _addr = {'address': _addr} %}
{% endif %}
{% if _addr.address == 'dhcp' %}
DHCP=yes
{% else %}
{% set _addr_cidr = (_addr.address ~ '/' ~ _addr.netmask | default('')).rstrip('/') | ansible.utils.ipaddr('host') %}
Address={{ _addr_cidr }}
{% endif %}
{% if _addr.gateway is defined and (_addr.gateway | length) > 0 %}
Gateway={{ item.1.gateway }}
{% endif %}
{% endfor %}
{% if item.1.bridge is defined %}
Bridge={{ item.1.bridge }}
{% elif item.1.bond is defined %}
Bond={{ item.1.bond }}
{% elif item.1.vlan is defined %}
{% if item.1.vlan is iterable and item.1.vlan is not string %}
{% for vlan in item.1.vlan %}
VLAN={{ vlan }}
{% endfor %}
{% else %}
VLAN={{ item.1.vlan }}
{% endif %}
{% elif item.1.macvlan is defined %}
MACVLAN={{ item.1.macvlan }}
{% elif item.1.vxlan is defined %}
VXLAN={{ item.1.vxlan }}
{% endif %}
{% if item.1.ipforward is defined %}
IPForward={{ (item.1.ipforward | bool) | ternary('yes', 'no') }}
{% endif %}
[Link]
{% if item.1.mtu is defined and item.1.mtu %}
MTUBytes={{ item.1.mtu }}
{% endif %}