Render wg0 config file with private key and subnet

This commit is contained in:
Khue Doan 2021-05-03 10:41:26 +07:00
parent 26d7fa3e2f
commit 6ef691eca4
4 changed files with 16 additions and 9 deletions

View File

@ -7,8 +7,5 @@
- hosts: all
become: yes
tasks:
- apt:
update_cache: yes
roles:
- name: wireguard

View File

@ -0,0 +1 @@
internal_subnet: 10.13.13.0

View File

@ -1,3 +1,7 @@
- name: Update apt cache
apt:
update_cache: yes
- name: Install Wireguard
apt:
name: wireguard
@ -8,11 +12,16 @@
creates: /etc/wireguard/privatekey
- name: Register private key
shell: cat /etc/wireguard/privatekey
slurp:
src: /etc/wireguard/privatekey
register: wireguard_private_key
changed_when: false
- name: Register public key
shell: cat /etc/wireguard/publickey
slurp:
src: /etc/wireguard/publickey
register: wireguard_public_key
changed_when: false
- name: Generate wg0 config
template:
src: wg0.conf.j2
dest: /etc/wireguard/wg0.conf

View File

@ -1,6 +1,6 @@
[Interface]
Address = {{ interface }}.1
Address = {{ internal_subnet | ansible.netcommon.ipmath(1) }}
ListenPort = 51820
PrivateKey = {{ wireguard_private_key }}
PrivateKey = {{ wireguard_private_key['content'] | b64decode }}
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE