diff options
author | Christian Pointner <equinox@spreadspace.org> | 2020-07-11 02:58:28 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2020-07-11 02:58:28 +0200 |
commit | 49c58d575f420165f7d8341bccb8b4ba4629e735 (patch) | |
tree | dfbfe6a937ec04fa7d48de8c1d08a1596ba837b2 /roles/vm/host | |
parent | Merge branch 'topic/debian-installer-verification' (diff) | |
parent | ch-gnocchi: remove temporary interface config (diff) |
Merge branch 'topic/vm-host-network'
Diffstat (limited to 'roles/vm/host')
-rw-r--r-- | roles/vm/host/base/handlers/main.yml (renamed from roles/vm/host/handlers/main.yml) | 0 | ||||
-rw-r--r-- | roles/vm/host/base/tasks/main.yml (renamed from roles/vm/host/tasks/main.yml) | 4 | ||||
-rw-r--r-- | roles/vm/host/base/tasks/zfs.yml (renamed from roles/vm/host/tasks/zfs.yml) | 0 | ||||
-rw-r--r-- | roles/vm/host/network/tasks/main.yml | 42 | ||||
-rw-r--r-- | roles/vm/host/network/templates/bridge-interfaces.j2 | 53 | ||||
-rw-r--r-- | roles/vm/host/network/templates/interfaces.j2 | 79 | ||||
-rw-r--r-- | roles/vm/host/tasks/network.yml | 75 |
7 files changed, 174 insertions, 79 deletions
diff --git a/roles/vm/host/handlers/main.yml b/roles/vm/host/base/handlers/main.yml index 6541dd80..6541dd80 100644 --- a/roles/vm/host/handlers/main.yml +++ b/roles/vm/host/base/handlers/main.yml diff --git a/roles/vm/host/tasks/main.yml b/roles/vm/host/base/tasks/main.yml index 4c29970d..1a7cb7d8 100644 --- a/roles/vm/host/tasks/main.yml +++ b/roles/vm/host/base/tasks/main.yml @@ -18,10 +18,6 @@ path: /etc/default/haveged notify: restart haveged -- name: install vm-host network - when: "'network' in vm_host" - include_tasks: network.yml - - name: prepare zfs volumes when: "'zfs' in vm_host" include_tasks: zfs.yml diff --git a/roles/vm/host/tasks/zfs.yml b/roles/vm/host/base/tasks/zfs.yml index b84f2d0d..b84f2d0d 100644 --- a/roles/vm/host/tasks/zfs.yml +++ b/roles/vm/host/base/tasks/zfs.yml diff --git a/roles/vm/host/network/tasks/main.yml b/roles/vm/host/network/tasks/main.yml new file mode 100644 index 00000000..cd415d1e --- /dev/null +++ b/roles/vm/host/network/tasks/main.yml @@ -0,0 +1,42 @@ +--- +- name: configure bonds and vlans + when: "'bonds' in network or 'vlans' in network" + block: + - name: install ifenslave package + when: "'bonds' in network" + apt: + name: ifenslave + state: present + + - name: install vlan package + when: "'vlans' in network" + apt: + name: vlan + state: present + + - name: create network interfaces + template: + src: interfaces.j2 + dest: /etc/network/interfaces + +- name: create network bridges + when: "'bridges' in vm_host.network" + block: + - name: generate bridge interface config + loop: "{{ vm_host.network.bridges | default({}) | dict2items }}" + loop_control: + label: "{{ item.key }}" + template: + src: bridge-interfaces.j2 + dest: "/etc/network/interfaces.d/br-{{ item.key }}" + register: vmhost_bridge_config + + ## We don't try to be to clever here: aka don't call ifdown before ifup because + ## if there are VMs running they would end up with a broken network + - name: bring up bridge interfaces + loop: "{{ vmhost_bridge_config.results }}" + loop_control: + label: "br-{{ item.item.key }}" + when: item is changed + command: "/sbin/ifup br-{{ item.item.key }}" + failed_when: false diff --git a/roles/vm/host/network/templates/bridge-interfaces.j2 b/roles/vm/host/network/templates/bridge-interfaces.j2 new file mode 100644 index 00000000..05144430 --- /dev/null +++ b/roles/vm/host/network/templates/bridge-interfaces.j2 @@ -0,0 +1,53 @@ +{% set bridge_name = 'br-'+item.key %} +{% set bridge = item.value %} +{% set interface = (network.interfaces | selectattr('name', 'eq', bridge_name) | first | default({})) %} +auto {{ bridge_name }} +{% if 'address' in interface %} +iface {{ bridge_name }} inet static + address {{ interface.address | ipaddr('address') }} + netmask {{ interface.address | ipaddr('netmask') }} +{% if 'gateway' in interface %} + gateway {{ interface.gateway }} +{% endif %} +{% else %} +iface {{ bridge_name }} inet manual +{% endif %} +{% if 'interfaces' in bridge and (bridge.interfaces | length) > 0 %} + bridge_ports {{ bridge.interfaces | join(' ') }} +{% else %} + bridge_ports none +{% endif %} + bridge_stp off + bridge_waitport 0 + bridge_fd 0 + up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/accept_ra + up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/autoconf + up modprobe br_netfilter + up /sbin/sysctl net.bridge.bridge-nf-call-iptables=0 + up /sbin/sysctl net.bridge.bridge-nf-call-ip6tables=0 + up /sbin/sysctl net.bridge.bridge-nf-call-arptables=0 +{% if 'address' in interface and 'prefix' in bridge %} +{% if 'nat' in bridge and bridge.nat %} + up echo 1 > /proc/sys/net/ipv4/conf/$IFACE/forwarding + up echo 1 > /proc/sys/net/ipv4/conf/{{ ansible_default_ipv4.interface }}/forwarding + up /sbin/iptables -t nat -A POSTROUTING -o {{ ansible_default_ipv4.interface }} -s {{ bridge.prefix }} -j SNAT --to {{ ansible_default_ipv4.address }} +{% endif %} +{% if 'overlay' in bridge %} +{% for dest, offset in (bridge.overlay.offsets | dictsort(by='value')) %} + up /bin/ip route add {{ (bridge.overlay.prefix | ipaddr(offset)).split('/')[0] }}/32 via {{ (bridge.prefix | ipaddr(bridge.offsets[dest])).split('/')[0] }} # {{ dest }} +{% endfor %} + up /bin/ip route add unreachable {{ bridge.overlay.prefix }} + down /sbin/ip route del {{ bridge.overlay.prefix }} +{% endif %} +{% if 'nat' in bridge and bridge.nat %} + down /sbin/iptables -t nat -D POSTROUTING -o {{ ansible_default_ipv4.interface }} -s {{ bridge.prefix }} -j SNAT --to {{ ansible_default_ipv4.address }} +{% endif %} +{% endif %} +{% if 'address6' in interface %} + +iface {{ bridge_name }} inet6 static + address {{ interface.address6 }} +{% if 'gateway6' in interface %} + gateway {{ interface.gateway6 }} +{% endif %} +{% endif %} diff --git a/roles/vm/host/network/templates/interfaces.j2 b/roles/vm/host/network/templates/interfaces.j2 new file mode 100644 index 00000000..fe57a024 --- /dev/null +++ b/roles/vm/host/network/templates/interfaces.j2 @@ -0,0 +1,79 @@ +# This file describes the network interfaces available on your system +# and how to activate them. For more information, see interfaces(5). + +# The loopback network interface +auto lo +iface lo inet loopback + + +## pyhiscal interfaces + +{% for interface in network.bonds | default([]) | map(attribute='slaves') | flatten | union(network.vlans | default({}) | list) | difference(network.bonds | default([]) | map(attribute='name') | list) | sort | unique %} +auto {{ interface }} +iface {{ interface }} inet manual + pre-up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/accept_ra + pre-up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/autoconf + +{% endfor %} + +{% for bond in network.bonds | default([]) %} +## Bond: {{ bond.name }} + +{% set tmp = network.interfaces | selectattr('name', 'eq', bond.name) | list %} +auto {{ bond.name }} +iface {{ bond.name }} inet {{ ((tmp | length) == 0) | ternary('manual', 'static') }} + bond-mode {{ bond.mode }} + bond-slaves {{ bond.slaves | sort | join(' ') }} +{% for option in (bond.options | default({}) | list | sort) %} + bond-{{ option }} {{ bond.options[option] }} +{% endfor %} + up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/accept_ra + up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/autoconf +{% if (tmp | length) > 0 %} +{% set interface = tmp | first %} + address {{ interface.address | ipaddr('address') }} + netmask {{ interface.address | ipaddr('netmask') }} +{% if 'gateway' in interface %} + gateway {{ interface.gateway }} +{% endif %} +{% for route in interface.static_routes | default([]) %} + up /bin/ip route add {{ route.destination }} via {{ route.gateway }} +{% endfor %} +{% for route in interface.static_routes | default([]) | reverse %} + down /bin/ip route del {{ route.destination }} via {{ route.gateway }} +{% endfor %} +{% if 'address6' in interface %} + +iface {{ interface.name }} inet6 static + address {{ interface.address6 }} +{% if 'gateway6' in interface %} + gateway {{ interface.gateway6 }} +{% endif %} +{% for route in interface.static_routes6 | default([]) %} + up /bin/ip -6 route add {{ route.destination }} via {{ route.gateway }} +{% endfor %} +{% for route in interface.static_routes6 | default([]) | reverse %} + down /bin/ip -6 route del {{ route.destination }} via {{ route.gateway }} +{% endfor %} +{% endif %} +{% endif %} + + +{% endfor %} +{% for parent in network.vlans | default({}) | list | sort %} +## vlan interfaces @ {{ parent }} + +{% for vlan in network.vlans[parent] %} +auto {{ parent }}.{{ vlan }} +iface {{ parent }}.{{ vlan }} inet manual + up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/accept_ra + up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/autoconf +{# TODO: add interface config like above if (network.interfaces | selectattr('name', 'eq', 'parent+'.'+vlan') | list) > 0 ... #} + +{% endfor %} + +{% endfor %} + +## source bridge configs + +source /etc/network/interfaces.d/* diff --git a/roles/vm/host/tasks/network.yml b/roles/vm/host/tasks/network.yml deleted file mode 100644 index 802ffd8b..00000000 --- a/roles/vm/host/tasks/network.yml +++ /dev/null @@ -1,75 +0,0 @@ ---- -- name: create network bridges - when: "'bridges' in vm_host.network" - block: - - name: generate bridge interface config - loop: "{{ vm_host.network.bridges | default({}) | dict2items }}" - loop_control: - label: "{{ item.key }}" - copy: - dest: "/etc/network/interfaces.d/br-{{ item.key }}" - content: | - {% set bridge_name = 'br-'+item.key %} - {% set bridge = item.value %} - {% set interface = (network.interfaces | selectattr('name', 'eq', bridge_name) | first | default({})) %} - auto {{ bridge_name }} - {% if 'address' in interface %} - iface {{ bridge_name }} inet static - address {{ interface.address | ipaddr('address') }} - netmask {{ interface.address | ipaddr('netmask') }} - {% if 'gateway' in interface %} - gateway {{ interface.gateway }} - {% endif %} - {% else %} - iface {{ bridge_name }} inet manual - {% endif %} - {% if 'interfaces' in bridge and (bridge.interfaces | length) > 0 %} - bridge_ports {{ bridge.interfaces | join(' ') }} - {% else %} - bridge_ports none - {% endif %} - bridge_stp off - bridge_waitport 0 - bridge_fd 0 - up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/accept_ra - up echo 0 > /proc/sys/net/ipv6/conf/$IFACE/autoconf - up modprobe br_netfilter - up /sbin/sysctl net.bridge.bridge-nf-call-iptables=0 - up /sbin/sysctl net.bridge.bridge-nf-call-ip6tables=0 - up /sbin/sysctl net.bridge.bridge-nf-call-arptables=0 - {% if 'address' in interface and 'prefix' in bridge %} - {% if 'nat' in bridge and bridge.nat %} - up echo 1 > /proc/sys/net/ipv4/conf/$IFACE/forwarding - up echo 1 > /proc/sys/net/ipv4/conf/{{ ansible_default_ipv4.interface }}/forwarding - up /sbin/iptables -t nat -A POSTROUTING -o {{ ansible_default_ipv4.interface }} -s {{ bridge.prefix }} -j SNAT --to {{ ansible_default_ipv4.address }} - {% endif %} - {% if 'overlay' in bridge %} - {% for dest, offset in (bridge.overlay.offsets | dictsort(by='value')) %} - up /bin/ip route add {{ (bridge.overlay.prefix | ipaddr(offset)).split('/')[0] }}/32 via {{ (bridge.prefix | ipaddr(bridge.offsets[dest])).split('/')[0] }} # {{ dest }} - {% endfor %} - up /bin/ip route add unreachable {{ bridge.overlay.prefix }} - down /sbin/ip route del {{ bridge.overlay.prefix }} - {% endif %} - {% if 'nat' in bridge and bridge.nat %} - down /sbin/iptables -t nat -D POSTROUTING -o {{ ansible_default_ipv4.interface }} -s {{ bridge.prefix }} -j SNAT --to {{ ansible_default_ipv4.address }} - {% endif %} - {% endif %} - {% if 'address6' in interface %} - - iface {{ bridge_name }} inet6 static - address {{ interface.address6 }} - {% if 'gateway6' in interface %} - gateway {{ interface.gateway6 }} - {% endif %} - {% endif %} - register: vmhost_bridge_config - - ## We don't try to be to clever here: aka don't call ifdown before ifup because - ## if there are VMs running they would end up with a broken network - - name: bring up bridge interfaces - loop: "{{ vmhost_bridge_config.results }}" - loop_control: - label: "br-{{ item.item.key }}" - when: item is changed - command: "/sbin/ifup br-{{ item.item.key }}" - failed_when: false |