From 9b2d0af2ef44351ed96c6fe77fffba3ea60f4681 Mon Sep 17 00:00:00 2001 From: Moritz 'e1mo' Fromm Date: Mon, 26 Oct 2020 21:57:54 +0100 Subject: [PATCH] Add default interface prefix for wireguard tunnels --- defaults/main.yml | 1 + tasks/wireguard.yml | 12 ++++++------ templates/bird2/peer.conf.j2 | 2 +- templates/wg-quick.j2 | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 2258c15..d704875 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,4 +1,5 @@ dn42_wg_private_key: "foobar2342" +dn42_wg_default_if_prefix: "dn42_" dn42_local_subnet_v4: "172.17.0.1/28" dn42_local_subnet_v6: "fe80::1/56" diff --git a/tasks/wireguard.yml b/tasks/wireguard.yml index 6c89b77..671e6a4 100644 --- a/tasks/wireguard.yml +++ b/tasks/wireguard.yml @@ -1,19 +1,19 @@ -- name: Install wg-quick@{{ peer.if.name | default("dn42_" + peer.name) }} config +- name: Install wg-quick@{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} config template: - dest: "/etc/wireguard/{{ peer.if.name | default('dn42_' + peer.name) }}.conf" + dest: "/etc/wireguard/{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }}.conf" src: "wg-quick.j2" when: "peer.wg is defined" register: "configuration" -- name: Enable wg-quick@{{ peer.if.name | default("dn42_" + peer.name) }} service +- name: Enable wg-quick@{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} service service: - name: "wg-quick@{{ peer.if.name | default('dn42_' + peer.name) }}" + name: "wg-quick@{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }}" enabled: yes -- name: Restart wg-quick@{{ peer.if.name | default('dn42_' + peer.name) }} +- name: Restart wg-quick@{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} service: - name: "wg-quick@{{ peer.if.name | default('dn42_' + peer.name) }}" + name: "wg-quick@{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }}" state: restarted when: "configuration is changed" diff --git a/templates/bird2/peer.conf.j2 b/templates/bird2/peer.conf.j2 index 55266c9..06003ef 100644 --- a/templates/bird2/peer.conf.j2 +++ b/templates/bird2/peer.conf.j2 @@ -7,7 +7,7 @@ protocol bgp {{ peer.name }}_v4 from dnpeers{% if not peer.multiprotocol is defi {% if peer.v6 is defined %} protocol bgp {{ peer.name }}{% if not peer.multiprotocol is defined %}_v6{% endif %} from dnpeers{% if not peer.multiprotocol is defined %}_v6{% endif %} { # if you use link-local ipv6 addresses for peering using the following - neighbor {{ peer.v6 }}%{{ peer.if.name | default('dn42_' + peer.name) }} as {{ peer.as }}; + neighbor {{ peer.v6 }}%{{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} as {{ peer.as }}; {% if peer.debug is defined %} debug {{ peer.debug }}; {% endif %} diff --git a/templates/wg-quick.j2 b/templates/wg-quick.j2 index 14e5309..5c80ff5 100644 --- a/templates/wg-quick.j2 +++ b/templates/wg-quick.j2 @@ -1,7 +1,7 @@ [Interface] PrivateKey = {{ peer.wg.privkey | default(dn42_wg_private_key) }} Address = {{ peer.if.v4 | default(dn42_local_v4) }}/32, {{ peer.if.v6 | default(dn42_local_v6) }}/128 -PostUp = {% if peer.v4 is defined %}/sbin/ip addr del dev {{ peer.if.name | default("dn42_" + peer.name) }} {{ peer.if.v4 | default(dn42_local_v4) }}/32 && /sbin/ip addr add dev {{ peer.if.name | default("dn42_" + peer.name) }} {{ peer.if.v4 | default(dn42_local_v4) }}/32 peer {{ peer.v4 }}/32 && {% endif %}{% if peer.v6 is defined %}/sbin/ip addr del dev {{ peer.if.name | default("dn42_" + peer.name) }} {{ peer.if.v6 | default(dn42_local_v6) }}/128 && /sbin/ip addr add dev {{ peer.if.name | default("dn42_" + peer.name) }} {{ peer.if.v6 | default(dn42_local_v6) }}/128 peer {{ peer.v6 }}/128{% endif %} +PostUp = {% if peer.v4 is defined %}/sbin/ip addr del dev {{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} {{ peer.if.v4 | default(dn42_local_v4) }}/32 && /sbin/ip addr add dev {{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} {{ peer.if.v4 | default(dn42_local_v4) }}/32 peer {{ peer.v4 }}/32 && {% endif %}{% if peer.v6 is defined %}/sbin/ip addr del dev {{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} {{ peer.if.v6 | default(dn42_local_v6) }}/128 && /sbin/ip addr add dev {{ peer.if.name | default(dn42_wg_default_if_prefix + peer.name) }} {{ peer.if.v6 | default(dn42_local_v6) }}/128 peer {{ peer.v6 }}/128{% endif %} Table = off ListenPort = {{ peer.wg.port }}