aboutsummaryrefslogtreecommitdiffstats
path: root/roles/wireguard/tasks/main.yml
blob: 98754576520b4e5abf7dcafd2ef63ffac49c8268 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86

# wireguard ~~ roles/wireguard/tasks/main.yml
# create keys and configuration for wireguard hosts

---

- name: generate dynamic facts
  include: set_facts.yml

- name: check keys on local disk
  stat:
    path: "{{ wg_host_keys }}"
  register: stat_host_keys
  delegate_to: localhost

- name: generate host keys
  shell: |
    umask 077
    wg genkey | tee "{{ wg_host_keys }}" | wg pubkey >> "{{ wg_host_keys }}"
  args:
    chdir: "{{ wg_dir }}"
  when: not stat_host_keys.stat.exists or force is defined and force
  delegate_to: localhost

- name: create wireguard dir on remote host
  file:
    path: /etc/wireguard
    owner: "{{ user_root }}"
    group: "{{ group_root }}"
    mode: 0700
    state: directory
  ignore_unreachable: true

- name: generate client configuration
  template:
    src: host.conf.j2
    dest: "{{ item.path }}"
    mode: 0600
  when: ansible_host != global.dcontroller
  delegate_to: "{{ item.name }}"
  loop:
    - { name: "{{ ansible_host }}", path: "/etc/wireguard/{{ global.dcontroller }}.conf" }
    - { name: localhost, path: "{{ wg_dir }}/{{ ansible_host }}.conf" }
  ignore_unreachable: true

- name: generate server configuration
  template:
    src: dcontroller.conf.j2
    dest: "{{ item.path }}"
    mode: "0600"
  when: ansible_host == global.dcontroller
  delegate_to: "{{ item.name }}"
  loop:
    - { name: "{{ ansible_host }}", path: "/etc/wireguard/{{ global.dcontroller }}.conf" }
    - { name: localhost, path: "{{ wg_dir }}/{{ ansible_host }}.conf" }

- name: enable wireguard for freebsd
  lineinfile:
    path: /etc/rc.conf
    regexp: "^wireguard_enable"
    line: wireguard_enable="YES"
  when: ansible_distribution == "FreeBSD"

- name: enable interface for freebsd
  lineinfile:
    path: /etc/rc.conf
    regexp: "^wireguard_interfaces"
    line: wireguard_interfaces="{{ global.dcontroller }}"
  when: ansible_distribution == "FreeBSd"

- name: generate and enable init script
  include_role: name=rc
  vars:
    rc_cmd: "{{ path_wgquick }}"
    rc_args: "up {{ global.dcontroller }}"
    rc_name: wireguard
    rc_user: "{{ user_root }}"
  ignore_errors: true
  when: ansible_distribution != "FreeBSD"

- name: generate server interface
  template:
    src: templates/hostname.j2
    dest: /etc/hostname.tun0
  when: ansible_host == global.dcontroller

remember that computers suck.