From 18b36137dcfa740ea9238bd2cbaaf0762667dbec Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Fri, 8 Dec 2023 18:26:43 +0100 Subject: [PATCH] Ensure filename is always defined for network/netdev Right now we define resulting filename in the loop itself, which limits ability to predict expected filename for another loop where list might get filtered. So to get more expected behaviour between tasks we always add filename to the elements in vars. Change-Id: I579980ea3a5e9349d25885530c979649cb6dcb81 --- defaults/main.yml | 9 +++++---- ...d_filename_formatting-ce5e032876cdca09.yaml | 12 ++++++++++++ tasks/main.yml | 14 +++++++------- vars/main.yml | 18 ++++++++++++++++++ 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 releasenotes/notes/networkd_filename_formatting-ce5e032876cdca09.yaml diff --git a/defaults/main.yml b/defaults/main.yml index b8f1d0f..423f7f6 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -27,10 +27,6 @@ systemd_networkd_prefix: "general" # Enable systemd-networkd and (re)start the service systemd_run_networkd: false -# Default filename formatting -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: # https://www.freedesktop.org/software/systemd/man/systemd.link.html @@ -75,6 +71,11 @@ 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 +# `filename` -- (Optional, Recommended) Defines resulted filename for config +# to be placed in. For consistency and more +# expected behaviour when list of networks +# may change it is recommended to supply +# filename for each interface. # `link_config_overrides` -- (Optional) Used to inject extra configuration # options into link file. A full list # of all options can be found here: diff --git a/releasenotes/notes/networkd_filename_formatting-ce5e032876cdca09.yaml b/releasenotes/notes/networkd_filename_formatting-ce5e032876cdca09.yaml new file mode 100644 index 0000000..531ccd5 --- /dev/null +++ b/releasenotes/notes/networkd_filename_formatting-ce5e032876cdca09.yaml @@ -0,0 +1,12 @@ +--- + +deprecations: + - | + Variables controlling systemd-networkd default filename templating when one + is not supplied were deprecated and has no effect from now on. + + * systemd_networkd_filename + * systemd_networkd_filename_alt + + It is highly recommended to provide ``filename`` parameter explicitly + whenever you define ``systemd_netdevs`` or ``systemd_networks`` structures. diff --git a/tasks/main.yml b/tasks/main.yml index e7fba80..ecf6ab6 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -143,11 +143,11 @@ - name: Create systemd-networkd network device(s) template: src: "systemd-netdev.j2" - dest: "/etc/systemd/network/{{ (item.1.filename | default(systemd_networkd_filename)) ~ '.netdev' }}" + dest: "/etc/systemd/network/{{ item.1.filename }}.netdev" owner: "root" group: "root" mode: "0644" - with_indexed_items: "{{ systemd_netdevs }}" + with_indexed_items: "{{ _systemd_netdevs_named }}" notify: - Restart systemd-networkd tags: @@ -156,13 +156,13 @@ - name: Create systemd-networkd network link(s) openstack.config_template.config_template: src: "systemd-link.j2" - dest: "/etc/systemd/network/{{ (item.1.filename | default(systemd_networkd_filename_alt)) ~ '.link' }}" + dest: "/etc/systemd/network/{{ item.1.filename }}.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 }}" + with_indexed_items: "{{ _systemd_networks_named }}" notify: - Update initramfs - Restart systemd-networkd @@ -172,13 +172,13 @@ - name: Create systemd-networkd network network(s) openstack.config_template.config_template: src: "systemd-network.j2" - dest: "/etc/systemd/network/{{ (item.1.filename | default(systemd_networkd_filename_alt)) ~ '.network' }}" + dest: "/etc/systemd/network/{{ item.1.filename }}.network" owner: "root" group: "root" mode: "0644" config_overrides: "{{ item.1.config_overrides | default({}) }}" config_type: "ini" - with_indexed_items: "{{ systemd_networks }}" + with_indexed_items: "{{ _systemd_networks_named }}" notify: - Restart systemd-networkd tags: @@ -206,7 +206,7 @@ sysctl_set: yes state: present reload: yes - with_indexed_items: "{{ systemd_networks }}" + with_indexed_items: "{{ _systemd_networks_named }}" when: - (ansible_facts['os_family'] | lower) == 'redhat' - item.1.ipforward | default(false) | bool diff --git a/vars/main.yml b/vars/main.yml index f78a119..2ce14da 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -15,3 +15,21 @@ ## APT Cache options cache_timeout: 600 + +_systemd_networks_named: |- + {% set named_networks = systemd_networks | selectattr('filename', 'defined') %} + {% for network in systemd_networks | rejectattr('filename', 'defined') %} + {% set network_filename = [loop.index0, systemd_networkd_prefix, network['interface']] %} + {% set _ = network.update({'filename': network_filename | join('-') }) %} + {% set _ = named_networks.append(network) %} + {% endfor %} + {{ named_networks }} + +_systemd_netdevs_named: |- + {% set named_netdevs = systemd_netdevs | selectattr('filename', 'defined') %} + {% for netdev in systemd_netdevs | rejectattr('filename', 'defined') %} + {% set netdev_filename = [loop.index0, systemd_networkd_prefix, netdev['NetDev']['Name']] %} + {% set _ = netdev.update({'filename': netdev_filename | join('-') }) %} + {% set _ = named_netdevs.append(netdev) %} + {% endfor %} + {{ named_netdevs }}