aboutsummaryrefslogtreecommitdiffstats
path: root/ansible/roles/wireguard/tasks/main.yml
blob: 9dc92dd6cf24e1ac67fb6de0eb3b6c6ca48ea76f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
- name: Install WireGuard
  apt:
    update_cache: true
    cache_valid_time: 3600
    pkg:
      - wireguard
      - wireguard-tools
      - linux-headers-{{ ansible_kernel }}
  tags:
    - role::wireguard

- name: Generate WireGuard private key
  shell: set -o pipefail && wg genkey > /etc/wireguard/key.priv
  args:
    executable: /bin/bash
    creates: /etc/wireguard/key.priv
  tags:
    - role::wireguard

- 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
  tags:
    - role::wireguard

- 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
  tags:
    - role::wireguard

- name: Fetch private key for all hosts
  slurp:
    src: /etc/wireguard/key.priv
  register: wg_priv_key
  tags:
    - role::wireguard

- name: Fetch public key for all hosts
  slurp:
    src: /etc/wireguard/key.pub
  register: wg_pub_key
  tags:
    - role::wireguard

- name: Generate WireGuard configuration file
  template:
    src: wg0.conf.j2
    dest: /etc/wireguard/wg0.conf
    mode: '0600'
    group: root
    owner: root
  notify:
    - Reload wg-quick
  tags:
    - role::wireguard

- name: Start and enable the WireGuard service
  service:
    name: wg-quick@wg0
    enabled: true
    state: started
  tags:
    - role::wireguard