2024-12-13 09:57:12 +00:00
|
|
|
---
|
|
|
|
- name: Cleanup expired VPN configuration
|
|
|
|
hosts: vpn_servers
|
|
|
|
become: yes
|
|
|
|
vars:
|
|
|
|
client_dir: /etc/wireguard/clients
|
2025-01-09 20:52:46 +00:00
|
|
|
test_client_dir: /etc/wireguard/test_clients
|
2024-12-13 09:57:12 +00:00
|
|
|
wg_interface: wg0
|
2025-01-09 20:52:46 +00:00
|
|
|
is_test: false # Default to production mode
|
2024-12-13 09:57:12 +00:00
|
|
|
|
|
|
|
tasks:
|
2025-01-09 20:52:46 +00:00
|
|
|
- name: Debug cleanup information
|
2024-12-13 09:57:12 +00:00
|
|
|
debug:
|
2025-01-09 20:52:46 +00:00
|
|
|
msg:
|
|
|
|
- "Cleaning up subscription ID: {{ subscription_id }}"
|
|
|
|
- "Test mode: {{ is_test }}"
|
|
|
|
|
|
|
|
# Set working directory based on mode
|
|
|
|
- name: Set working directory based on mode
|
|
|
|
set_fact:
|
|
|
|
working_client_dir: "{{ test_client_dir if is_test else client_dir }}"
|
|
|
|
|
2024-12-13 09:57:12 +00:00
|
|
|
- name: Remove client configuration directory
|
|
|
|
file:
|
2025-01-09 20:52:46 +00:00
|
|
|
path: "{{ working_client_dir }}/{{ subscription_id }}"
|
2024-12-13 09:57:12 +00:00
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Remove client from server config
|
|
|
|
blockinfile:
|
|
|
|
path: "/etc/wireguard/{{ wg_interface }}.conf"
|
|
|
|
marker: "# {mark} ANSIBLE MANAGED BLOCK FOR {{ subscription_id }}"
|
|
|
|
state: absent
|
|
|
|
notify: restart wireguard
|
2025-01-09 20:52:46 +00:00
|
|
|
|
|
|
|
# Remove cleanup cron job if it exists (for test configs)
|
|
|
|
- name: Remove cleanup cronjob
|
|
|
|
when: is_test
|
|
|
|
cron:
|
|
|
|
name: "cleanup_test_vpn_{{ subscription_id }}"
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Log cleanup
|
|
|
|
shell: |
|
|
|
|
logger -t vpn-cleanup "Cleaned up VPN configuration for {{ subscription_id }} ({{ 'test' if is_test else 'production' }})"
|
2024-12-13 09:57:12 +00:00
|
|
|
|
|
|
|
handlers:
|
|
|
|
- name: restart wireguard
|
|
|
|
service:
|
|
|
|
name: "wg-quick@{{ wg_interface }}"
|
|
|
|
state: restarted
|