~nka/ansible-role-wireguard

aeae7424a51270cffe97019b11d0d3c6d8dbdbdf — Nicolas KAROLAK 2 months ago master v1.0.0
init
5 files changed, 135 insertions(+), 0 deletions(-)

A defaults/main.yml
A handlers/main.yml
A meta/main.yml
A tasks/main.yml
A templates/wg.conf.j2
A  => defaults/main.yml +14 -0
@@ 1,14 @@
---

wg_packages:
  - wireguard

wg_conf: wg0
wg_private_key:
wg_public_key:
wg_address: 192.168.14.1/24
wg_port: 51820

wg_peers: []

...

A  => handlers/main.yml +6 -0
@@ 1,6 @@
---

- name: restart wireguard
  systemd:
    name: wg-quick@{{ wg_conf }}
    state: restarted

A  => meta/main.yml +14 -0
@@ 1,14 @@
---

galaxy_info:
  author: Nicolas Karolak
  description: Install a Wireguard server
  galaxy_tags: []
  license: WTFPL
  min_ansible_version: 2.9
  platforms:
    - name: 'Debian'
      versions:
        - 'buster'

dependencies: []

A  => tasks/main.yml +91 -0
@@ 1,91 @@
---

# INSTALL

- name: add backports repository
  apt_repository:
    repo: deb http://deb.debian.org/debian/ buster-backports main contrib non-free
    filename: backports

- name: install linux headers
  apt:
    force_apt_get: true
    install_recommends: false
    name: linux-headers-amd64

- name: install packages
  apt:
    force_apt_get: true
    install_recommends: false
    name: "{{ wg_packages }}"

# GENERATE KEYS

- name: generate private key
  when: not wg_private_key | d()
  shell:
    cmd: |
      wg genkey > /etc/wireguard/privatekey
      chmod 600 /etc/wireguard/privatekey
    creates: /etc/wireguard/privatekey

- name: retrieve private key content
  when: not wg_private_key | d()
  register: wg_private_key_b64
  slurp:
    src: /etc/wireguard/privatekey

- name: register private key as variable
  when: not wg_private_key | d()
  set_fact:
    wg_private_key: "{{ wg_private_key_b64['content'] | b64decode | trim }}"

- name: generate public key
  when: not wg_public_key | d()
  shell:
    cmd: echo {{ wg_private_key }} | wg pubkey > /etc/wireguard/publickey
    creates: /etc/wireguard/publickey

- name: retrieve public key content
  when: not wg_public_key | d()
  register: wg_public_key_b64
  slurp:
    src: /etc/wireguard/publickey

- name: register public key as variable
  when: not wg_public_key | d()
  set_fact:
    wg_public_key: "{{ wg_public_key_b64['content'] | b64decode | trim }}"

# CONFIGURE

- name: configure server
  notify: restart wireguard
  template:
    src: wg.conf.j2
    dest: /etc/wireguard/{{ wg_conf }}.conf

- name: allow ipv4 forwarding
  sysctl:
    sysctl_file: /etc/sysctl.d/wg.conf
    name: net.ipv4.ip_forward
    value: "1"
    sysctl_set: true
    reload: true
    state: present

- name: allow ipv6 forwarding
  sysctl:
    sysctl_file: /etc/sysctl.d/wg.conf
    name: net.ipv6.conf.all.forwarding
    value: "1"
    sysctl_set: true
    reload: true
    state: present

- name: enable service
  systemd:
    name: wg-quick@{{ wg_conf }}
    enabled: true

...

A  => templates/wg.conf.j2 +10 -0
@@ 1,10 @@
[Interface]
PrivateKey = {{ wg_private_key | mandatory }}
ListenPort = {{ wg_port }}
Address = {{ wg_address }}
{% for peer in wg_peers %}

[Peer]
PublicKey = {{ peer.public_key }}
AllowedIPs = {{ peer.ip }}
{% endfor %}