aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--roles/wireguard/tasks/main.yml54
-rw-r--r--roles/wireguard/templates/wg0.conf.j215
2 files changed, 69 insertions, 0 deletions
diff --git a/roles/wireguard/tasks/main.yml b/roles/wireguard/tasks/main.yml
new file mode 100644
index 0000000..66d35a4
--- /dev/null
+++ b/roles/wireguard/tasks/main.yml
@@ -0,0 +1,54 @@
+- name: Install WireGuard
+ apt:
+ update_cache: true
+ cache_valid_time: 3600
+ pkg:
+ - wireguard
+ - wireguard-tools
+ - linux-headers-{{ ansible_kernel }}
+
+- name: Generate WireGuard private key
+ shell: set -o pipefail && wg genkey > /etc/wireguard/key.priv
+ args:
+ executable: /bin/bash
+ creates: /etc/wireguard/key.priv
+
+- name: Generate WireGuard public key
+ shell: set -o pipefail && cat /etc/wireguard/key.priv | wg pubkey > /etc/wireguard/key.pub
+ args:
+ executable: /bin/bash
+ creates: /etc/wireguard/key.pub
+
+- name: Ensure file permissions for keys set correctly
+ file:
+ path: '{{ item }}'
+ owner: root
+ group: root
+ mode: '0600'
+ with_items:
+ - /etc/wireguard/key.priv
+ - /etc/wireguard/key.pub
+
+- name: Fetch private key for all hosts
+ slurp:
+ src: /etc/wireguard/key.priv
+ register: wg_priv_key
+
+- name: Fetch public key for all hosts
+ slurp:
+ src: /etc/wireguard/key.pub
+ register: wg_pub_key
+
+- name: Generate WireGuard configuration file
+ template:
+ src: wg0.conf.j2
+ dest: /etc/wireguard/wg0.conf
+ mode: '0600'
+ group: root
+ owner: root
+
+- name: Start and enable the WireGuard service
+ service:
+ name: wg-quick@wg0
+ enabled: true
+ state: started
diff --git a/roles/wireguard/templates/wg0.conf.j2 b/roles/wireguard/templates/wg0.conf.j2
new file mode 100644
index 0000000..b42b1fc
--- /dev/null
+++ b/roles/wireguard/templates/wg0.conf.j2
@@ -0,0 +1,15 @@
+# Configuration managed by Ansible
+[Interface]
+Address = {{ wireguard_subnet }}
+ListenPort = 46850
+PrivateKey = {{ wg_priv_key['content'] | b64decode | trim }}
+
+{% for host in hostvars.keys() if not host == inventory_hostname %}
+# Peer config for: {{ host }}
+[Peer]
+AllowedIPs = {{ hostvars[host]['wireguard_subnet'] }}
+PublicKey = {{ hostvars[host]['wg_pub_key']['content'] | b64decode | trim }}
+Endpoint = {{ host }}.box.pydis.wtf:46850
+PersistentKeepalive = 30
+
+{% endfor %}