Replace usage of networkd template with role

For consistency reasons instead of placing a template for
systemd-networkd we better use systemd_networkd role that is present
for a while.

Change-Id: I1e9deaa2892a8fa7eb171acaf75441d7efeac297
This commit is contained in:
Dmitriy Rabotyagov 2022-10-14 15:50:46 +02:00 committed by Dmitriy Rabotyagov
parent 0b7a8645de
commit 517b75ac61
5 changed files with 25 additions and 79 deletions

View File

@ -57,22 +57,6 @@
changed_when: false
listen: Lxc container restart
- name: Flush addresses
command: "ip addr flush {{ item.value.interface }}"
with_dict: "{{ lxc_container_networks_combined }}"
failed_when: false
when:
- item.value.interface is defined
listen: Restart networkd
- name: Restart systemd-networkd
systemd:
name: systemd-networkd
state: restarted
enabled: true
daemon_reload: true
listen: Restart networkd
- name: Enable resolved
systemd:
name: systemd-resolved

View File

@ -210,7 +210,7 @@
changed_when: wiring_script.rc == 3
delegate_to: "{{ physical_host }}"
- include_tasks: "lxc_container_network_new.yml"
- include_tasks: "lxc_container_network.yml"
# VETH AND CONNECTIVITY SETTINGS

View File

@ -13,23 +13,14 @@
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Create networkd directory
file:
path: "/etc/systemd/network"
state: directory
- name: Drop container network file (interfaces)
template:
src: "container_network.network.j2"
dest: "/etc/systemd/network/{{ item.value.interface }}.network"
owner: "root"
group: "root"
mode: "0644"
with_dict: "{{ lxc_container_networks_combined }}"
notify:
- Enable dbus
- Enable resolved
- Restart networkd
- name: Run the systemd-networkd role
import_role:
name: systemd_networkd
vars:
systemd_run_networkd: true
systemd_networkd_distro_packages: []
systemd_networks: "{{ _lxc_container_systemd_networks }}"
- name: Create resolved link
file:

View File

@ -1,46 +0,0 @@
[Match]
Name={{ item.value.interface }}
{% if item.value.address is defined %}
[Address]
{% set addr_cidr = (item.value.address | string + '/' + item.value.netmask | string) | ansible.utils.ipaddr('prefix') %}
Address={{ item.value.address }}/{{ addr_cidr }}
{% else %}
[DHCP]
{% if item.value.dhcp_use_dns is defined %}
UseDNS={{ item.value.dhcp_use_dns | bool | lower }}
{% else %}
UseDNS={{ (lxc_container_enable_resolved | bool) | ternary('yes', 'no') }}
{% endif %}
UseNTP=yes
RouteMetric=20
{% if hostvars[inventory_hostname]['gateway'] is defined
or item.value.dhcp_use_routes is defined %}
UseRoutes={{ item.value.dhcp_use_routes | default(false) | bool | lower }}
{% endif %}
{% endif %}
{% for route in item.value.static_routes | default([]) %}
[Route]
Destination={{ route['cidr'] }}
Gateway={{ route['gateway'] }}
Metric={{ 20 + loop.index }}
{% endfor %}
[Network]
{% if item.value.address is defined %}
{% set addr_cidr = (item.value.address | string + '/' + item.value.netmask | string) | ansible.utils.ipaddr('prefix') %}
Address={{ item.value.address }}/{{ addr_cidr }}
{% else %}
DHCP=yes
{% endif %}
{% if item.value.gateway is defined %}
Gateway={{ item.value.gateway }}
{% endif %}
[Link]
{% if item.value.mtu is defined %}
MTUBytes={{ item.value.mtu }}
{% endif %}

View File

@ -33,3 +33,20 @@ lxc_container_default_config_list:
lxc_container_networks_combined: "{{ container_networks | default({}) | combine(container_extra_networks | default({})) | combine(lxc_container_networks) }}"
sysctl_path: "/sbin"
_lxc_container_bind_mounts: []
_lxc_container_systemd_networks: |
{% set systemd_networks = [] %}
{% for network in lxc_container_networks_combined.values() %}
{% set _ = systemd_networks.append({
'filename': network.interface,
'interface': network.interface,
'address': network.address | default('dhcp'),
'netmask': network.netmask | default(),
'gateway': network.gateway | default(),
'mtu': network.mtu | default(),
'static_routes': network.static_routes | default([]),
'usedns': ('dhcp_use_dns' in network) | ternary(network.dhcp_use_dns, lxc_container_enable_resolved | bool),
'config_overrides': ('dhcp_use_routes' in network) | ternary({'DHCP': network.dhcp_use_routes}, {})
}) %}
{% endfor %}
{{ systemd_networks }}