summaryrefslogtreecommitdiffstats
path: root/roles/wireguard/tasks/main.yml
blob: a0f0cca14f105d41d858c0a04d799bf7054daba6 (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
---
- name: "Include OS-specific variables"
  ansible.builtin.include_vars: "{{ ansible_os_family | lower }}.yml"

- name: "Ensure wireguard is installed"
  become: true
  ansible.builtin.package:
    name: "{{ wireguard_pkgs }}"
    state: "present"

- name: "Deploy WireGuard configurations"
  become: true
  ansible.builtin.copy:
    content: "{{ wireguard_connections[item] }}"
    dest: "/etc/wireguard/{{ item }}.conf"
    owner: "root"
    group: "root"
    mode: "0600"
    backup: true
  with_items: "{{ wireguard_connections.keys() }}"
  when: wireguard_connections | length > 0
  tags:
    - molecule-idempotence-notest

- name: "Configure autostart connection"
  become: true
  when: wireguard_autostart_connection != ""
  block:
    - name: "Ensure all wireguard connections are stopped"
      ansible.builtin.systemd_service:
        name: "wg-quick@{{ item }}"
        enabled: false
        state: "stopped"
      with_items: "{{ wireguard_connections.keys() | list }}"

    - name: "Ensure all wireguard interfaces are down"
      ansible.builtin.command:
        cmd: "wg-quick down {{ item }}"
      with_items: "{{ wireguard_connections.keys() | list }}"
      changed_when: false
      failed_when: false

    - name: "Enable and start wg-quick service for connection {{ wireguard_autostart_connection }}"
      ansible.builtin.systemd_service:
        name: "wg-quick@{{ wireguard_autostart_connection }}"
        enabled: true
        state: "started"