diff --git a/playbooks/statistica.yml b/playbooks/statistica.yml index d4028de..a7a9726 100644 --- a/playbooks/statistica.yml +++ b/playbooks/statistica.yml @@ -1,5 +1,5 @@ --- -- name: Collect Debian system metrics and send to Telegram (with optional proxy) +- name: Collect Debian system metrics and send to Telegram (via SOCKS5 proxy) hosts: all gather_facts: yes become: true @@ -7,9 +7,10 @@ vars: telegram_token: "8820292312:AAECFO1AEWOJTh30zNuhyjC5C81o_qJHQcM" telegram_chat_id: "407968638" - # Прокси: socks5h://user:pass@host:port — оставь пустым, если не нужен - proxy: "" - # Пакеты, нужные для метрик (Debian) + # SOCKS5-прокси на control node (машинке, откуда запускается Ansible) + telegram_proxy: "socks5h://administrator:60NthRegT@192.168.0.218:1080" + + # Пакеты для метрик (Debian) metrics_packages: - curl - bc @@ -19,7 +20,7 @@ - smartmontools tasks: - # ---------- 0. PREFLIGHT: ставим зависимости на Debian ---------- + # ---------- 0. PREFLIGHT: зависимости на Debian ---------- - name: Ensure required packages are installed (Debian/Ubuntu) ansible.builtin.apt: name: "{{ metrics_packages }}" @@ -27,7 +28,6 @@ update_cache: yes cache_valid_time: 3600 when: ansible_os_family == "Debian" - register: apt_result - name: Warn if host is not Debian family ansible.builtin.debug: @@ -262,15 +262,33 @@ 🔥 Топ-5 процессов по памяти:
{{ top_procs_joined }}
- # ---------- 14. Отправка в Telegram ----------
- - name: Send to Telegram via proxy
+ # ---------- 14. Проверка доступности Telegram через прокси ----------
+ - name: Check Telegram API reachability via SOCKS5 proxy (from control node)
+ ansible.builtin.shell: |
+ curl -s -o /dev/null -w "%{http_code}" --max-time 15 \
+ --proxy "{{ telegram_proxy }}" \
+ "https://api.telegram.org/bot{{ telegram_token }}/getMe"
+ register: tg_check
+ changed_when: false
+ failed_when: false
+ delegate_to: localhost
+ become: false
+
+ - name: Fail early if Telegram API is unreachable via proxy
+ ansible.builtin.fail:
+ msg: >
+ Telegram API недоступен через прокси {{ telegram_proxy }}.
+ HTTP-код: {{ tg_check.stdout }}. Проверь прокси, учётку и сеть.
+ when: tg_check.stdout != "200"
+ delegate_to: localhost
+ become: false
+
+ # ---------- 15. Отправка в Telegram через SOCKS5-прокси ----------
+ - name: Send to Telegram via SOCKS5 proxy (from control node)
ansible.builtin.shell: |
set -e
- proxy_opt=""
- if [ -n "{{ proxy }}" ]; then
- proxy_opt="--socks5-hostname {{ proxy }}"
- fi
- response=$(curl -s -w "\n%{http_code}" -X POST $proxy_opt \
+ response=$(curl -s -w "\n%{http_code}" -X POST \
+ --proxy "{{ telegram_proxy }}" \
-H "Content-Type: application/json" \
-d '{"chat_id": "{{ telegram_chat_id }}", "text": {{ message_html | to_json }}, "parse_mode": "HTML"}' \
"https://api.telegram.org/bot{{ telegram_token }}/sendMessage" 2>&1)