Обновить playbooks/check_vpn.yml

This commit is contained in:
2026-08-20 12:07:26 +03:00
parent c1758ebe15
commit 86aeefa388
+91 -10
View File
@@ -1,11 +1,92 @@
- name: Check and reconnect AdGuard VPN - name: Check and reconnect AdGuard VPN with fallback
hosts: raspberry # или имя хоста из инвентори hosts: raspberry
tasks: gather_facts: false
- name: Get VPN status vars:
command: adguardvpn-cli status locations:
register: vpn_status - Helsinki
changed_when: false - Tallinn
- Riga
- Vilnius
- Warsaw
- Stockholm
- Copenhagen
- Berlin
- Frankfurt
- Amsterdam
- London
- name: Reconnect if disconnected tasks:
command: adguardvpn-cli connect - name: Get current VPN status
when: "'Disconnected' in vpn_status.stdout" command: adguardvpn-cli status
register: status_output
changed_when: false
ignore_errors: true
- name: Display current status
debug:
msg: "Current VPN status: {{ status_output.stdout | default('Unknown') }}"
- name: Determine if connected
set_fact:
vpn_connected: "{{ 'Connected' in (status_output.stdout | default('')) }}"
- name: Attempt to connect or reconnect with fallback
shell: |
# Функция для логирования с временем
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Если уже подключены – выходим
if echo "{{ vpn_connected }}" | grep -q "True"; then
log "Already connected, nothing to do."
exit 0
fi
# Функция проверки статуса
check_status() {
adguardvpn-cli status | grep -q "Connected"
}
# Попытка простого подключения
log "Trying simple connect (without location)..."
timeout 30 adguardvpn-cli connect
sleep 5
if check_status; then
log "Successfully connected via simple connect"
exit 0
fi
log "Simple connect failed or timed out."
# Перебор заданных локаций
for city in {{ locations | join(' ') }}; do
log "Trying to connect to '$city'..."
timeout 30 adguardvpn-cli connect -l "$city"
sleep 5
if check_status; then
log "Successfully connected to $city"
exit 0
fi
log "Connection to $city failed."
done
log "All attempts failed."
exit 1
when: not vpn_connected
register: reconnect_result
ignore_errors: true
timeout: 600
- name: Display full reconnect log
debug:
var: reconnect_result.stdout_lines # или stdout, но lines показывает по строкам
- name: Final status check
command: adguardvpn-cli status
register: final_status
changed_when: false
ignore_errors: true
- name: Display final status
debug:
msg: "Final VPN status: {{ final_status.stdout | default('Unknown') }}"