Make wireguard role generic

This commit is contained in:
Ivan R. 2024-10-30 17:42:17 +05:00
parent 05b13685e4
commit d39ea3b25f
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
5 changed files with 33 additions and 8 deletions

View file

@ -0,0 +1,12 @@
---
argument_specs:
main:
options:
interface:
type: str
listen_port:
type: int
address:
type: str
peers:
type: list

View file

@ -10,7 +10,8 @@
ansible.builtin.file:
path: /etc/wireguard
state: directory
mode: '1750'
mode: '1700'
owner: root
- name: Generate private key
become: true
ansible.builtin.shell: |
@ -44,9 +45,10 @@
ansible.builtin.template:
src: wg0.conf.j2
dest: /etc/wireguard/wg0.conf
register: cfg
- name: Enable wireguard service
become: true
ansible.builtin.systemd_service:
name: wg-quick@wg0
state: restarted
state: "{% if cfg.changed %}restarted{% else %}started{% endif %}"
enabled: true

View file

@ -1,10 +1,12 @@
[Interface]
PrivateKey = {{ private_key.content | b64decode }}
Address = 10.110.0.1/24
ListenPort = 51840
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ens18 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ens18 -j MASQUERADE
Address = {{ address }}
ListenPort = {{ listen_port }}
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o {{ interface }} -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o {{ interface }} -j MASQUERADE
{% for peer in peers %}
[Peer]
PublicKey = awAVP/tkl0Z9PKEMTABjIXhblWSGHhIvYjBFp3C7YUk=
AllowedIPs = 10.110.0.2/32
PublicKey = {{ peer.public_key }}
AllowedIPs = {{ peer.allowed_ips }}
{% endfor %}

View file

@ -0,0 +1,4 @@
interface: ens18
listen_port: 51820
address: 10.0.0.1/8
peers: []

View file

@ -2,3 +2,8 @@
- hosts: network
roles:
- role: wireguard
address: 10.110.0.1/24
listen_port: 51840
peers:
- public_key: awAVP/tkl0Z9PKEMTABjIXhblWSGHhIvYjBFp3C7YUk=
allowed_ips: 10.110.0.2/32