xoxys.wireguard/tasks/main.yml

71 lines
2.0 KiB
YAML
Raw Normal View History

2022-10-11 07:32:13 +00:00
---
2024-02-18 19:45:29 +00:00
- name: Ensure dependencies are installed
ansible.builtin.package:
name: "{{ item }}"
state: present
loop:
- wireguard-tools
2022-10-11 07:32:13 +00:00
2024-02-18 19:45:29 +00:00
- name: Stat WireGuard config file
ansible.builtin.stat:
path: "/etc/wireguard/{{ wireguard_interface }}.conf"
register: __wireguard_config_file
2022-10-11 07:32:13 +00:00
2024-02-18 19:45:29 +00:00
- when:
- not __wireguard_config_file.stat.exists
- wireguard_private_key is not defined
block:
2022-10-11 07:32:13 +00:00
- name: Generate WireGuard private key
2024-02-18 19:45:29 +00:00
ansible.builtin.command: "wg genkey"
2022-10-11 07:32:13 +00:00
register: __wireguard_private_key_gen
2022-10-11 07:48:42 +00:00
changed_when: False
2022-10-11 07:32:13 +00:00
2022-10-11 07:48:42 +00:00
- name: Set generated private key
2024-02-18 19:45:29 +00:00
ansible.builtin.set_fact:
2022-10-11 07:32:13 +00:00
wireguard_private_key: "{{ __wireguard_private_key_gen.stdout }}"
2024-02-18 19:45:29 +00:00
- when:
- __wireguard_config_file.stat.exists
- wireguard_private_key is not defined
block:
2022-10-11 07:32:13 +00:00
- name: Read WireGuard config file
2024-02-18 19:45:29 +00:00
ansible.builtin.slurp:
2022-10-11 07:32:13 +00:00
src: "/etc/wireguard/{{ wireguard_interface }}.conf"
register: __wireguard_config
2022-10-11 07:48:42 +00:00
- name: Set existing private key
2024-02-18 19:45:29 +00:00
ansible.builtin.set_fact:
2022-10-11 07:32:13 +00:00
wireguard_private_key: "{{ __wireguard_config['content'] | b64decode | regex_findall('PrivateKey = (.*)') | first }}"
2024-02-18 19:45:29 +00:00
- name: Derive WireGuard public key
ansible.builtin.command: "wg pubkey"
args:
stdin: "{{ wireguard_private_key }}"
register: __wireguard_public_key_gen
changed_when: False
2022-10-11 07:32:13 +00:00
2024-02-18 19:45:29 +00:00
- name: Set public key fact
ansible.builtin.set_fact:
__wireguard_public_key: "{{ __wireguard_public_key_gen.stdout }}"
2022-10-11 07:32:13 +00:00
2024-02-18 19:45:29 +00:00
- name: Generate WireGuard configuration file
ansible.builtin.template:
src: etc/wireguard/wg.conf.j2
dest: "/etc/wireguard/{{ wireguard_interface }}.conf"
owner: root
group: root
mode: "0600"
notify: __wireguard_restart
2022-10-11 07:32:13 +00:00
2024-02-18 19:45:29 +00:00
- name: Ensure legacy reload-module-on-update is absent
ansible.builtin.file:
dest: "/etc/wireguard/.reload-module-on-update"
state: absent
2022-10-11 07:32:13 +00:00
2024-02-18 19:45:29 +00:00
- name: Ensure WireGuard service is up and running
ansible.builtin.service:
name: "wg-quick@{{ wireguard_interface }}"
daemon_reload: True
enabled: True
state: started