summaryrefslogtreecommitdiffstats
path: root/roles/wireguard/tasks/main.yml
blob: 83517972ea2ce731ad196ecccb2c34f07bcc3720 (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
---

- name: Install WireGuard
  package:
    name: "{{ item }}"
    state: present
  with_items:
    - wireguard-lts
    - wireguard-tools

- name: Ensure /etc/wireguard exists
  file:
    path: "/etc/wireguard"
    state: directory
    owner: root
    group: root
    mode: 0700

- name: Generate private key(s)
  shell: 'wg genkey'
  register: wireguard_keys
  when: item.privateKey is not defined
  no_log: true
  with_items: "{{ wireguard }}"

- name: Set private key(s)
  set_fact:
    wireguard: "{{ [wireguard|combine(
      item|combine({'privateKey': wireguard_keys.results[index].stdout})
    )] }}"
  when: item.privateKey is not defined
  no_log: true
  loop: "{{ wireguard }}"
  loop_control:
    index_var: index

- name: Install configuration files
  template:
    src: wg.conf.j2
    dest: "/etc/wireguard/{{ item.name }}.conf"
    owner: root
    group: root
    mode: 0600
  with_items: "{{ wireguard }}"
  register: wireguard_changed
  no_log: true
  notify:
    - restart wireguard

- name: Start and enable wireguard service
  systemd:
    name: "wg-quick@{{ item.name }}.service"
    daemon-reload: yes
    state: started
    enabled: True
  no_log: true
  with_items: "{{ wireguard }}"