diff --git a/defaults/main.yml b/defaults/main.yml index ee9bd18..2ac47b2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -28,8 +28,8 @@ systemd_networkd_prefix: "general" systemd_run_networkd: false # Default filename formatting -systemd_networkd_netdev_filename: "{{ item.0 }}-{{ systemd_networkd_prefix }}-{{ item.1.NetDev.Name }}.netdev" -systemd_networkd_network_filename: "{{ item.0 }}-{{ systemd_networkd_prefix }}-{{ item.1.interface }}.network" +systemd_networkd_filename: "{{ item.0 }}-{{ systemd_networkd_prefix }}-{{ item.1.NetDev.Name }}" +systemd_networkd_filename_alt: "{{ item.0 }}-{{ systemd_networkd_prefix }}-{{ item.1.interface }}" # The `systemd_link_config_overrides` option can be used on the default link. # See the following link for all available options: @@ -75,6 +75,10 @@ systemd_netdevs: [] # into the network file. A full list of all # options can be found here: # https://www.freedesktop.org/software/systemd/man/systemd.network.html +# `link_config_overrides` -- (Optional) Used to inject extra configuration +# options into link file. A full list +# of all options can be found here: +# https://www.freedesktop.org/software/systemd/man/systemd.link.html # `interface` -- (required) Name of interface to match # `address` -- (option) IP address the interface should be given. To make this # interface use DHCP set this string to "dhcp" @@ -148,3 +152,10 @@ systemd_networkd_update_initramfs: "{{ _systemd_networkd_update_initramfs | defa # - systemd-resolved systemd_networkd_distro_packages: "{{ _systemd_networkd_distro_packages | default([]) }}" + +# The systemd networkd service can be used to set specific link configuration options. +# For more information on all of the possible configuration see +# https://www.freedesktop.org/software/systemd/man/systemd.link.html + +systemd_networkd_link: + MACAddressPolicy: "persistent" diff --git a/tasks/main.yml b/tasks/main.yml index 0b7f1e0..7d4268f 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -46,21 +46,6 @@ tags: - systemd-networkd -- name: Create systemd-networkd link - config_template: - src: "systemd-network-link.j2" - dest: "/etc/systemd/network/99-default.link" - owner: "root" - group: "root" - mode: "0644" - config_overrides: "{{ systemd_link_config_overrides }}" - config_type: "ini" - notify: - - Restart systemd-networkd - - Update initramfs - tags: - - systemd-networkd - - name: Create systemd-resolved config template: src: "systemd-resolved.conf.j2" @@ -100,7 +85,7 @@ - name: Create systemd-networkd network device(s) template: src: "systemd-netdev.j2" - dest: "/etc/systemd/network/{{ item.1.filename | default(systemd_networkd_netdev_filename) }}" + dest: "/etc/systemd/network/{{ (item.1.filename | default(systemd_networkd_filename)) ~ '.netdev' }}" owner: "root" group: "root" mode: "0644" @@ -110,10 +95,26 @@ tags: - systemd-networkd -- name: Create systemd-networkd network(s) +- name: Create systemd-networkd network link(s) + config_template: + src: "systemd-link.j2" + dest: "/etc/systemd/network/{{ (item.1.filename | default(systemd_networkd_filename_alt)) ~ '.link' }}" + owner: "root" + group: "root" + mode: "0644" + config_overrides: "{{ item.1.link_config_overrides | default(systemd_link_config_overrides) }}" + config_type: "ini" + with_indexed_items: "{{ systemd_networks }}" + notify: + - Update initramfs + - Restart systemd-networkd + tags: + - systemd-networkd + +- name: Create systemd-networkd network network(s) config_template: src: "systemd-network.j2" - dest: "/etc/systemd/network/{{ item.1.filename | default(systemd_networkd_network_filename) }}" + dest: "/etc/systemd/network/{{ (item.1.filename | default(systemd_networkd_filename_alt)) ~ '.network' }}" owner: "root" group: "root" mode: "0644" diff --git a/templates/systemd-link.j2 b/templates/systemd-link.j2 new file mode 100644 index 0000000..d19fd34 --- /dev/null +++ b/templates/systemd-link.j2 @@ -0,0 +1,13 @@ +# {{ ansible_managed }} + +[Match] +Name={{ item.1.interface }} + +[Link] +{% if item.1.mtu is defined %} +MTUBytes={{ item.1.mtu }} +{% endif %} + +{% for key, value in systemd_networkd_link.items() %} +{{ key }}={{ value }} +{% endfor %} diff --git a/templates/systemd-network-link.j2 b/templates/systemd-network-link.j2 deleted file mode 100644 index 147d224..0000000 --- a/templates/systemd-network-link.j2 +++ /dev/null @@ -1,5 +0,0 @@ -# {{ ansible_managed }} - -[Link] -NamePolicy=kernel database onboard slot path -MACAddressPolicy=persistent diff --git a/templates/systemd-network.j2 b/templates/systemd-network.j2 index 41de9c3..2a11519 100644 --- a/templates/systemd-network.j2 +++ b/templates/systemd-network.j2 @@ -55,8 +55,3 @@ MACVLAN={{ item.1.macvlan }} {% elif item.1.vxlan is defined %} VXLAN={{ item.1.vxlan }} {% endif %} - -[Link] -{% if item.1.mtu is defined %} -MTUBytes={{ item.1.mtu }} -{% endif %} diff --git a/tests/test.yml b/tests/test.yml index 643d046..53175f7 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -22,6 +22,9 @@ roles: - role: "systemd_networkd" vars: + systemd_networkd_link: + MACAddressPolicy: "persistent" + NamePolicy: "kernel database onboard slot path" systemd_run_networkd: yes systemd_resolved: DNS: "208.67.222.222" @@ -64,7 +67,7 @@ - interface: "br-dummy" address: "10.0.0.100" netmask: "255.255.255.0" - gateway: "10.0.0.1" + gateway: "{{ ansible_default_ipv4.gateway | default('10.0.0.1') }}" mtu: 9000 usedns: true static_routes: @@ -73,6 +76,8 @@ config_overrides: Network: ConfigureWithoutCarrier: true + link_config_overrides: + Alias: "dummy-bridge0" - interface: "dummy2" bridge: "br-test" - interface: "br-test" @@ -117,6 +122,15 @@ - ansible_br_test['type'] == 'bridge' - ansible_br_test['ipv4']['address'] == '10.1.0.1' - ansible_br_test['ipv4']['netmask'] == '255.255.255.0' + - name: Check link config overrides + shell: 'grep -wo "Alias" /etc/systemd/network/*br-dummy.link' + changed_when: false + - name: Check general link config + shell: 'grep -wo "{{ item }}" /etc/systemd/network/*.link' + changed_when: false + with_items: + - MACAddressPolicy + - NamePolicy - name: Playbook for role testing with cleanup