From 076074fa02f4ba33f798c2053771e4827fd1c35f Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 22 Jul 2020 23:01:37 +0200 Subject: add some more interface configs --- inventory/host_vars/ch-oulu-vm1.yml | 11 ++++++++++- .../network/templates/interfaces/multihomed-base.j2 | 18 ++++++++++++++++++ .../templates/interfaces/multihomed-overlay.j2 | 19 +++++++++++++++++++ .../vm/guest/network/templates/interfaces/overlay.j2 | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 roles/vm/guest/network/templates/interfaces/multihomed-base.j2 create mode 100644 roles/vm/guest/network/templates/interfaces/multihomed-overlay.j2 diff --git a/inventory/host_vars/ch-oulu-vm1.yml b/inventory/host_vars/ch-oulu-vm1.yml index b760cde5..a02a1d77 100644 --- a/inventory/host_vars/ch-oulu-vm1.yml +++ b/inventory/host_vars/ch-oulu-vm1.yml @@ -26,7 +26,16 @@ network: name: lan0 address: "{{ network_zones.lan.prefix | ipaddr(network_zones.lan.offsets[inventory_hostname]) | ipaddr('address/prefix') }}" gateway: "{{ network_zones.lan.gateway }}" + config: multihomed-base interfaces: - *_network_primary_ - name: svc0 - address: 1.2.3.4/24 + address: 192.168.3.1/24 + config: multihomed-overlay + overlays: + - name: mur.at + address: 1.2.3.4/28 + gateway: 1.2.3.1 + - name: magenta + address: 5.6.7.8/27 + gateway: 5.6.7.1 diff --git a/roles/vm/guest/network/templates/interfaces/multihomed-base.j2 b/roles/vm/guest/network/templates/interfaces/multihomed-base.j2 new file mode 100644 index 00000000..db16d15f --- /dev/null +++ b/roles/vm/guest/network/templates/interfaces/multihomed-base.j2 @@ -0,0 +1,18 @@ + address {{ interface.address | ipaddr('address') }} + netmask {{ interface.address | ipaddr('netmask') }} +{% for route in interface.static_routes | default([]) %} + up /bin/ip route add {{ route.destination }} via {{ route.gateway }}{% if 'source' in route %} src {{ route.source }}{% endif %}{% if 'table' in route %} table {{ route.table }}{% endif %} +{% endfor %} +{% if 'gateway' in interface %} + up /bin/ip route add default via {{ interface.gateway }} table default +{% endif %} + up /bin/ip rule add pref 42000 lookup default + up /bin/ip rule del pref 32767 + down /bin/ip rule add pref 32767 lookup default + down /bin/ip rule del pref 42000 +{% if 'gateway' in interface %} + down /bin/ip route del default via {{ interface.gateway }} table default +{% endif %} +{% for route in interface.static_routes | default([]) | reverse %} + down /bin/ip route del {{ route.destination }} via {{ route.gateway }}{% if 'source' in route %} src {{ route.source }}{% endif %}{% if 'table' in route %} table {{ route.table }}{% endif %} +{% endfor %} diff --git a/roles/vm/guest/network/templates/interfaces/multihomed-overlay.j2 b/roles/vm/guest/network/templates/interfaces/multihomed-overlay.j2 new file mode 100644 index 00000000..7ac94246 --- /dev/null +++ b/roles/vm/guest/network/templates/interfaces/multihomed-overlay.j2 @@ -0,0 +1,19 @@ + address {{ interface.address | ipaddr('address') }} + netmask {{ interface.address | ipaddr('netmask') }} +{% for overlay in interface.overlays %} +{% if 'name' in overlay %} + ## {{ overlay.name }} +{% endif %} + up /bin/ip addr add dev $IFACE {{ overlay.address }} + up /bin/ip route add default via {{ overlay.gateway }} src {{ overlay.address | ipaddr('address') }} table {{ 200 + loop.index0 }} + up /bin/ip rule add pref {{ 40000 + loop.index0 }} from {{ overlay.address | ipaddr('address') }} lookup {{ 200 + loop.index0 }} +{% endfor %} + ########## +{% for overlay in interface.overlays | reverse %} +{% if 'name' in overlay %} + ## {{ overlay.name }} +{% endif %} + down /bin/ip rule del pref {{ 40000 + loop.revindex0 }} + down /bin/ip route del default via {{ overlay.gateway }} src {{ overlay.address | ipaddr('address') }} table {{ 200 + loop.revindex0 }} + down /bin/ip addr del dev $IFACE {{ overlay.address }} +{% endfor %} diff --git a/roles/vm/guest/network/templates/interfaces/overlay.j2 b/roles/vm/guest/network/templates/interfaces/overlay.j2 index a3262f9c..0524c5df 100644 --- a/roles/vm/guest/network/templates/interfaces/overlay.j2 +++ b/roles/vm/guest/network/templates/interfaces/overlay.j2 @@ -7,8 +7,8 @@ {% if 'gateway' in interface %} up /bin/ip route add default via {{ interface.gateway }} src {{ interface.overlay }} down /bin/ip route del default via {{ interface.gateway }} src {{ interface.overlay }} +{% endif %} {% for route in interface.static_routes | default([]) | reverse %} down /bin/ip route del {{ route.destination }} via {{ route.gateway }}{% if 'source' in route %} src {{ route.source }}{% endif %} {% endfor %} -{% endif %} down /bin/ip addr del dev $IFACE {{ interface.overlay }}/32 -- cgit v1.2.3