- tags: - lxc-host - lxc-host-network become: yes vars: file_prefix: "/etc/systemd/network/{{ lxc_host__networkd_number }}-lxc-host" br_if: "{{ lxc_host__br_if }}" internal_if: "{{ host_database[ansible_hostname].interfaces[lxc_host__internal_if] }}" block: - debug: var=br_if - debug: var=internal_if - name: enable systemd-networkd service: name: systemd-networkd enabled: yes state: started when: lxc_host__state == "present" - loop: - "{{ file_prefix }}-1-{{ br_if }}.netdev" - "{{ file_prefix }}-2-{{ br_if }}.network" - "{{ file_prefix }}-3-{{ lxc_host__internal_if }}.netdev" - "{{ file_prefix }}-4-{{ lxc_host__internal_if }}.network" when: lxc_host__state == "absent" notify: systemctl restart systemd-networkd file: path: "{{ item }}" state: absent - name: "{{ file_prefix }}-1-{{ br_if }}.netdev" notify: systemctl restart systemd-networkd when: lxc_host__state == "present" copy: dest: "{{ file_prefix }}-1-{{ br_if }}.netdev" content: | [NetDev] Name={{ br_if }} Kind=bridge - name: "{{ file_prefix }}-2-{{ br_if }}.network" notify: systemctl restart systemd-networkd when: lxc_host__state == "present" copy: dest: "{{ file_prefix }}-2-{{ br_if }}.network" content: | [Match] Name={{ br_if }} [Network] {% if internal_if.ipv4 is defined %} Address={{ internal_if.ipv4.address }}/{{ internal_if.ipv4.netmask }} {% if internal_if.ipv4.gateway is defined %} Gateway={{ internal_if.ipv4.gateway }} {% endif %} {% endif %} {% if internal_if.ipv6 is defined %} Address={{ internal_if.ipv6.address }}/{{ internal_if.ipv6.netmask }} {% endif %} {% if internal_if.dns is defined %} {% for dns in internal_if.dns %} DNS={{ dns }} {% endfor %} {% endif %} - name: "{{ file_prefix }}-3-{{ lxc_host__internal_if }}.netdev" notify: systemctl restart systemd-networkd when: lxc_host__state == "present" copy: dest: "{{ file_prefix }}-3-{{ lxc_host__internal_if }}.netdev" content: | [NetDev] Name={{ lxc_host__internal_if }} Kind=dummy - name: "{{ file_prefix }}-4-{{ lxc_host__internal_if }}.network" notify: systemctl restart systemd-networkd when: lxc_host__state == "present" copy: dest: "{{ file_prefix }}-4-{{ lxc_host__internal_if }}.network" content: | [Match] Name={{ lxc_host__internal_if }} [Network] Bridge={{ br_if }} - meta: flush_handlers - name: Configure sysctl, enable ipv4 and ipv6 forwarding for {{ br_if }} with_items: - net.ipv4.conf.{{ br_if }}.forwarding - net.ipv6.conf.{{ br_if }}.forwarding sysctl: name: "{{ item }}" value: 1 state: "{{ lxc_host__state }}"