diff options
| author | Ahmed Abdelhalim <[email protected]> | 2025-08-20 23:37:21 +0200 |
|---|---|---|
| committer | Ahmed Abdelhalim <[email protected]> | 2025-08-21 00:46:50 +0200 |
| commit | f93dd15e52140d8e2fb5462ed009a954f1885572 (patch) | |
| tree | 96bb58b08b54997669e6a9c415a53a7f4d1bd277 /roles/wireguard/tasks | |
| parent | 36ec6f1a3961abbbb41495dd4d141871f6675fea (diff) | |
Add wireguard role and fix testing
The testing was failing because the use of the example files with the
same domain names, resulted in the files and the molecule variable were
being merged and therefore running tasks that would fail on test
(example, setting a fake VPN connection that wouldn't start).
Diffstat (limited to 'roles/wireguard/tasks')
| -rw-r--r-- | roles/wireguard/tasks/main.yml | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/roles/wireguard/tasks/main.yml b/roles/wireguard/tasks/main.yml new file mode 100644 index 00000000..45011ea1 --- /dev/null +++ b/roles/wireguard/tasks/main.yml @@ -0,0 +1,54 @@ +--- +- 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: "Create wireguard configuration directory" + become: true + ansible.builtin.file: + path: "/etc/wireguard" + state: "directory" + owner: "root" + group: "root" + mode: "0700" + +- 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 }}" + when: wireguard_connections | length > 0 + +- name: "Configure autostart connection" + become: true + block: + - name: "Ensure all wireguard connections are stopped" + ansible.builtin.systemd: + 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: true + failed_when: false + + - name: "Enable and start wg-quick service for connection {{ wireguard_autostart_connection }}" + ansible.builtin.systemd: + name: "wg-quick@{{ wireguard_autostart_connection }}" + enabled: true + state: "started" + when: wireguard_autostart_connection != "" |
