mirror of
				https://github.com/kevinveenbirkenbach/computer-playbook.git
				synced 2025-10-31 02:10:05 +00:00 
			
		
		
		
	Another big round of refactoring and cleaning...
This commit is contained in:
		
							
								
								
									
										24
									
								
								roles/sys-alm-telegram/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								roles/sys-alm-telegram/README.md
									
									
									
									
									
										Normal 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 Telegram’s 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). | ||||
							
								
								
									
										4
									
								
								roles/sys-alm-telegram/handlers/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								roles/sys-alm-telegram/handlers/main.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,4 @@ | ||||
| - name: "restart sys-alm-telegram service" | ||||
|   systemd: | ||||
|     name: sys-alm-telegram.cymais.service | ||||
|     daemon_reload: yes | ||||
							
								
								
									
										26
									
								
								roles/sys-alm-telegram/meta/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								roles/sys-alm-telegram/meta/main.yml
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										30
									
								
								roles/sys-alm-telegram/tasks/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								roles/sys-alm-telegram/tasks/main.yml
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										14
									
								
								roles/sys-alm-telegram/templates/sys-alm-telegram.sh.j2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								roles/sys-alm-telegram/templates/sys-alm-telegram.sh.j2
									
									
									
									
									
										Normal 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" | ||||
| @@ -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 | ||||
							
								
								
									
										2
									
								
								roles/sys-alm-telegram/vars/main.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								roles/sys-alm-telegram/vars/main.yml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | ||||
| systemd_telegram_folder: /opt/ansible-roles/sys-alm-telegram/ | ||||
| systemd_telegram_script: '{{systemd_telegram_folder}}sys-alm-telegram.sh' | ||||
		Reference in New Issue
	
	Block a user