Telegram alerts: accept credentials from extra vars or environment, report which is missing
Semaphore variable groups can pass values as extra vars or as environment variables, and secrets can be either type. Read telegram_bot_token, TELEGRAM_BOT_TOKEN (extra var) or TELEGRAM_BOT_TOKEN (env), same for the chat id, and say in the "not configured" message which of the two is missing so a wrong box or a detached variable group is visible in the log. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XEizHawueRXUcxQELRZ8kq
This commit is contained in:
@@ -6,8 +6,8 @@
|
||||
#
|
||||
# Extra vars (optional):
|
||||
# restart_stopped - default false; when true, start services found stopped
|
||||
# telegram_bot_token - or environment variable TELEGRAM_BOT_TOKEN
|
||||
# telegram_chat_id - or environment variable TELEGRAM_CHAT_ID
|
||||
# telegram_bot_token / TELEGRAM_BOT_TOKEN - extra var or environment variable
|
||||
# telegram_chat_id / TELEGRAM_CHAT_ID - extra var or environment variable
|
||||
|
||||
- name: Check Host Availability
|
||||
hosts: ubuntu:rocky
|
||||
@@ -92,9 +92,11 @@
|
||||
|
||||
vars:
|
||||
restart_stopped: false
|
||||
telegram_bot_token: "{{ lookup('env', 'TELEGRAM_BOT_TOKEN') }}"
|
||||
telegram_chat_id: "{{ lookup('env', 'TELEGRAM_CHAT_ID') }}"
|
||||
telegram_configured: "{{ telegram_bot_token | length > 0 and telegram_chat_id | length > 0 }}"
|
||||
# Accepted from a Semaphore variable group as extra vars (telegram_bot_token
|
||||
# or TELEGRAM_BOT_TOKEN) or as environment variables (TELEGRAM_BOT_TOKEN).
|
||||
telegram_bot_token: "{{ TELEGRAM_BOT_TOKEN | default(lookup('env', 'TELEGRAM_BOT_TOKEN'), true) | string }}"
|
||||
telegram_chat_id: "{{ TELEGRAM_CHAT_ID | default(lookup('env', 'TELEGRAM_CHAT_ID'), true) | string }}"
|
||||
telegram_configured: "{{ telegram_bot_token | string | length > 0 and telegram_chat_id | string | length > 0 }}"
|
||||
|
||||
tasks:
|
||||
- name: Build alert text
|
||||
@@ -116,7 +118,11 @@
|
||||
|
||||
- name: Print alerts when Telegram is not configured
|
||||
ansible.builtin.debug:
|
||||
msg: "ALERTS (Telegram not configured - set telegram_bot_token and telegram_chat_id):\n{{ alert_text }}"
|
||||
msg: |-
|
||||
ALERTS (Telegram not configured - bot token {{ 'set' if telegram_bot_token | string | length > 0 else 'MISSING' }},
|
||||
chat id {{ 'set' if telegram_chat_id | string | length > 0 else 'MISSING' }};
|
||||
pass TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID through the variable group attached to this template)
|
||||
{{ alert_text }}
|
||||
when:
|
||||
- alert_text | length > 0
|
||||
- not telegram_configured
|
||||
|
||||
Reference in New Issue
Block a user