~nka/ansible-role-wireguard-client

e0b919bb56f609d1fa645d4a235494ec453548d9 — Nicolas KAROLAK 2 months ago master v1.0.0
init
5 files changed, 118 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 +15 -0
@@ 1,15 @@
---

wg_packages:
  - wireguard

wg_conf: wg0
wg_private_key:
wg_public_key:
wg_address:
wg_port: 51820

wg_endpoint_address:
wg_endpoint_port: 51820

...

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 client
  galaxy_tags: []
  license: WTFPL
  min_ansible_version: 2.9
  platforms:
    - name: 'Debian'
      versions:
        - 'buster'

dependencies: []

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

# 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 client
  notify: restart wireguard
  template:
    src: wg.conf.j2
    dest: /etc/wireguard/{{ wg_conf }}.conf

- 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 | mandatory }}

[Peer]
PublicKey = {{ wg_endpoint_public_key | mandatory }}
Endpoint = {{ wg_endpoint_address | mandatory }}:{{ wg_endpoint_port | mandatory }}
AllowedIPs = {{ wg_address }}, 0.0.0.0/5, 8.0.0.0/7, 11.0.0.0/8, 12.0.0.0/6, 16.0.0.0/4, 32.0.0.0/3, 64.0.0.0/2, 128.0.0.0/3, 160.0.0.0/5, 168.0.0.0/6, 172.0.0.0/12, 172.32.0.0/11, 172.64.0.0/10, 172.128.0.0/9, 173.0.0.0/8, 174.0.0.0/7, 176.0.0.0/4, 192.0.0.0/9, 192.128.0.0/11, 192.160.0.0/13, 192.169.0.0/16, 192.170.0.0/15, 192.172.0.0/14, 192.176.0.0/12, 192.192.0.0/10, 193.0.0.0/8, 194.0.0.0/7, 196.0.0.0/6, 200.0.0.0/5, 208.0.0.0/4
PersistentKeepalive = 25