- name: Check and reconnect AdGuard VPN with fallback hosts: raspberry gather_facts: false vars: locations: - Helsinki - Tallinn - Riga - Vilnius - Warsaw - Stockholm - Copenhagen - Berlin - Frankfurt - Amsterdam - London tasks: - name: Get current VPN status 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') }}"