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
This commit is contained in:
Dmitriy Rabotyagov 2023-12-08 18:26:43 +01:00 committed by Dmitriy Rabotyagov
parent 1e6fc97dbe
commit 18b36137dc
4 changed files with 42 additions and 11 deletions

View File

@ -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:

View File

@ -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.

View File

@ -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

View File

@ -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 }}