diff --git a/handlers/main.yml b/handlers/main.yml index 6a8138d..e85e12f 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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 diff --git a/tasks/lxc_container_config.yml b/tasks/lxc_container_config.yml index 61a332b..f428f29 100644 --- a/tasks/lxc_container_config.yml +++ b/tasks/lxc_container_config.yml @@ -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 diff --git a/tasks/lxc_container_network_new.yml b/tasks/lxc_container_network.yml similarity index 65% rename from tasks/lxc_container_network_new.yml rename to tasks/lxc_container_network.yml index d0b51a9..3a926a5 100644 --- a/tasks/lxc_container_network_new.yml +++ b/tasks/lxc_container_network.yml @@ -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: diff --git a/templates/container_network.network.j2 b/templates/container_network.network.j2 deleted file mode 100644 index 62f4f59..0000000 --- a/templates/container_network.network.j2 +++ /dev/null @@ -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 %} diff --git a/vars/main.yml b/vars/main.yml index 976b730..9d638d9 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -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 }}