diff --git a/playbooks/update.yml b/playbooks/update.yml index eeeaab3..7567486 100644 --- a/playbooks/update.yml +++ b/playbooks/update.yml @@ -4,6 +4,14 @@ become: yes gather_facts: yes + vars: + # Telegram bot token (из задания) + telegram_token: "8820292312:AAECFO1AEWOJTh30zNuhyjC5C81o_qJHQcM" + # ID чата, куда отправлять уведомления (из вашего сообщения) + telegram_chat_id: "407968638" + # Прокси для Telegram API (формат для curl) + telegram_proxy: "socks5h://administrator:60NthRegT@192.168.0.218:1080" + tasks: # 1. Показываем текущую версию ОС и ядра (для контекста) - name: Show OS and kernel version @@ -73,4 +81,35 @@ - name: Reboot not required debug: msg: "No reboot required." - when: not reboot_required_file.stat.exists \ No newline at end of file + when: not reboot_required_file.stat.exists + + # ===== ОПОВЕЩЕНИЕ В TELEGRAM ===== + - name: Build notification message + set_fact: + message_text: | + Host: {{ inventory_hostname }} + OS: {{ ansible_distribution }} {{ ansible_distribution_version }} + Kernel: {{ ansible_kernel }} + Update result: {% if apt_upgrade_result.changed %}✅ Updates applied{% else %}✅ No updates needed{% endif %} + {% if apt_upgrade_result.changed and upgradable_before.stdout_lines is defined and upgradable_before.stdout_lines | length > 0 %} + Upgraded packages (old → new): + {{ upgradable_before.stdout_lines | select('match', '^[^/]+/') | list | join('\n') }} + {% endif %} + Reboot required: {% if reboot_required_file.stat.exists %}⚠️ YES{% else %}No{% endif %} + + - name: Send Telegram notification + shell: | + # Экранируем текст для JSON + MESSAGE=$(cat <<'EOF' | python3 -c 'import sys, json; print(json.dumps(sys.stdin.read()))' + {{ message_text }} + EOF + ) + curl -x {{ telegram_proxy }} \ + -s -X POST \ + -H "Content-Type: application/json" \ + -d "{\"chat_id\": \"{{ telegram_chat_id }}\", \"text\": $MESSAGE, \"parse_mode\": \"HTML\"}" \ + https://api.telegram.org/bot{{ telegram_token }}/sendMessage + when: + - telegram_token | default('') != '' + - telegram_chat_id | default('') != '' + ignore_errors: yes \ No newline at end of file