Another big round of refactoring and cleaning...

This commit is contained in:
2025-07-11 17:55:26 +02:00
parent aa61bf2a44
commit 168c5c0da6
323 changed files with 761 additions and 811 deletions

View File

@@ -0,0 +1,24 @@
# Automated Telegram Alerts for Service Failures
## Description
This role installs and configures the necessary components for sending notifications via systemd when a service fails. It sets up the `sys-alm-telegram` service and configures parameters and customizable templates for sending messages through [Telegram](https://telegram.org).
## Overview
Optimized for real-time alerts, this role is a key component of the overall [`sys-alm-compose` suite](../). It ensures that, upon failure of a critical service, a Telegram message is automatically sent to notify administrators and enable prompt troubleshooting.
## Purpose
The primary purpose of this role is to provide a robust solution for automated Telegram notifications in a systemd environment. By integrating with Telegrams Bot API and using customizable message templates, it delivers clear and timely alerts about service failures, thereby enhancing system observability and reliability.
## Features
- **Service Installation & Configuration:** Installs and configures necessary components (including the `curl` package).
- **Customizable Templates:** Supports tailored Telegram message templates for service failure notifications.
- **Secure Notifications:** Leverages systemd to trigger alerts automatically when services fail.
- **Suite Integration:** Part of the [`sys-alm-compose` suite](../) which includes related roles such as [sys-alm-email](../sys-alm-email/README.md) and others.
## Other Resources
This role was developed as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba).

View File

@@ -0,0 +1,4 @@
- name: "restart sys-alm-telegram service"
systemd:
name: sys-alm-telegram.cymais.service
daemon_reload: yes

View File

@@ -0,0 +1,26 @@
---
galaxy_info:
author: "Kevin Veen-Birkenbach"
description: "Installs and configures components for sending Telegram notifications through systemd. This role is part of the sys-alm-compose suite, providing automated alerts when services fail."
license: "CyMaIS NonCommercial License (CNCL)"
license_url: "https://s.veen.world/cncl"
company: |
Kevin Veen-Birkenbach
Consulting & Coaching Solutions
https://www.veen.world
min_ansible_version: "2.9"
platforms:
- name: Archlinux
versions:
- rolling
galaxy_tags:
- telegram
- systemd
- notifications
- automation
- curl
repository: "https://s.veen.world/cymais"
issue_tracker_url: "https://s.veen.world/cymaisissues"
documentation: "https://s.veen.world/cymais"
dependencies:
- sys-rst-daemon

View File

@@ -0,0 +1,30 @@
- name: install curl
pacman:
name: curl
state: present
when: run_once_systemd_notifier_telegram is not defined
- name: Create a directory with a subdirectory
ansible.builtin.file:
path: "{{systemd_telegram_folder}}"
state: directory
mode: '0755'
when: run_once_systemd_notifier_telegram is not defined
- name: configure sys-alm-telegram.sh
template:
src: sys-alm-telegram.sh.j2
dest: "{{ systemd_telegram_script }}"
when: run_once_systemd_notifier_telegram is not defined
- name: configure sys-alm-telegram.cymais.service
template:
src: sys-alm-telegram@.service.j2
dest: "/etc/systemd/system/sys-alm-telegram.cymais@.service"
notify: "restart sys-alm-telegram service"
when: run_once_systemd_notifier_telegram is not defined
- name: run the systemd_notifier_telegram tasks once
set_fact:
run_once_systemd_notifier_telegram: true
when: run_once_systemd_notifier_telegram is not defined

View File

@@ -0,0 +1,14 @@
#!/bin/bash
# determine host name: try hostname command, otherwise use $HOSTNAME
if command -v hostname &>/dev/null; then
host=$(hostname)
else
host="$HOSTNAME"
fi
# send the Telegram message
/usr/bin/curl -s -X POST \
"https://api.telegram.org/bot{{ telegram_bot_token }}/sendMessage" \
-d chat_id="{{ telegram_chat_id }}" \
-d text="service $1 on ${host} failed"

View File

@@ -0,0 +1,8 @@
[Unit]
Description=status Telegram message for %i to user
[Service]
Type=oneshot
ExecStart=/bin/bash {{ systemd_telegram_script }} %i
User=root
Group=systemd-journal

View File

@@ -0,0 +1,2 @@
systemd_telegram_folder: /opt/ansible-roles/sys-alm-telegram/
systemd_telegram_script: '{{systemd_telegram_folder}}sys-alm-telegram.sh'