From c14ce9d08a07e402bb2775ac5ac8112a13186dc4 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 28 May 2023 22:17:33 +0200 Subject: [PATCH] Implemented telegram notifications --- .../templates/backup-to-usb.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/backups-cleanup.service.j2 | 2 +- roles/independent_systemd-email/README.md | 6 --- .../handlers/main.yml | 4 -- .../independent_systemd-email/tasks/main.yml | 18 -------- roles/independent_systemd-email/vars/main.yml | 1 - .../meta/main.yml | 5 ++- .../templates/keyboard-color.service.j2 | 1 + roles/pc_system-swapfile/tasks/main.yml | 2 +- roles/server_docker-mailu/meta/main.yml | 2 +- .../deploy-letsencrypt-mailu.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/pull-remote-backups.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/btrfs-health-check.service.j2 | 2 +- .../server_native-certbot-nginx/meta/main.yml | 2 +- .../templates/certbot.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/disc-space-check.service.j2 | 2 +- ...ocker-compose-restart-unhealthy.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/docker-health-check.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/docker-volume-backup.service.j2 | 2 +- .../meta/main.yml | 2 +- .../templates/free-disc-space.service.j2 | 2 +- .../meta/main.yml | 2 +- .../journalctl-health-check.service.j2 | 2 +- roles/systemd_notifier/README.md | 37 ++++++++++++++++ roles/systemd_notifier/handlers/main.yml | 4 ++ roles/systemd_notifier/meta/main.yml | 4 ++ roles/systemd_notifier/tasks/main.yml | 6 +++ .../templates/systemd-notifier@.service.j2 | 8 ++++ roles/systemd_notifier_email/README.md | 44 +++++++++++++++++++ .../systemd_notifier_email/handlers/main.yml | 4 ++ roles/systemd_notifier_email/tasks/main.yml | 18 ++++++++ .../templates/msmtprc.conf.j2 | 0 .../templates/systemd_notifier_email.sh.j2} | 0 .../systemd_notifier_email@.service.j2} | 2 +- roles/systemd_notifier_email/vars/main.yml | 1 + roles/systemd_notifier_telegram/README.md | 41 +++++++++++++++++ .../handlers/main.yml | 4 ++ .../systemd_notifier_telegram/tasks/main.yml | 21 +++++++++ .../templates/systemd_notifier_telegram.sh.j2 | 2 + .../systemd_notifier_telegram@.service.j2 | 8 ++++ roles/systemd_notifier_telegram/vars/main.yml | 2 + 47 files changed, 232 insertions(+), 55 deletions(-) delete mode 100644 roles/independent_systemd-email/README.md delete mode 100644 roles/independent_systemd-email/handlers/main.yml delete mode 100644 roles/independent_systemd-email/tasks/main.yml delete mode 100644 roles/independent_systemd-email/vars/main.yml create mode 100644 roles/systemd_notifier/README.md create mode 100644 roles/systemd_notifier/handlers/main.yml create mode 100644 roles/systemd_notifier/meta/main.yml create mode 100644 roles/systemd_notifier/tasks/main.yml create mode 100644 roles/systemd_notifier/templates/systemd-notifier@.service.j2 create mode 100644 roles/systemd_notifier_email/README.md create mode 100644 roles/systemd_notifier_email/handlers/main.yml create mode 100644 roles/systemd_notifier_email/tasks/main.yml rename roles/{independent_systemd-email => systemd_notifier_email}/templates/msmtprc.conf.j2 (100%) rename roles/{independent_systemd-email/templates/systemd-email.sh.j2 => systemd_notifier_email/templates/systemd_notifier_email.sh.j2} (100%) rename roles/{independent_systemd-email/templates/systemd-email@.service.j2 => systemd_notifier_email/templates/systemd_notifier_email@.service.j2} (55%) create mode 100644 roles/systemd_notifier_email/vars/main.yml create mode 100644 roles/systemd_notifier_telegram/README.md create mode 100644 roles/systemd_notifier_telegram/handlers/main.yml create mode 100644 roles/systemd_notifier_telegram/tasks/main.yml create mode 100644 roles/systemd_notifier_telegram/templates/systemd_notifier_telegram.sh.j2 create mode 100644 roles/systemd_notifier_telegram/templates/systemd_notifier_telegram@.service.j2 create mode 100644 roles/systemd_notifier_telegram/vars/main.yml diff --git a/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 b/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 index aaf9f90e..ad82599c 100644 --- a/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 +++ b/roles/independent_backup-to-usb/templates/backup-to-usb.service.j2 @@ -2,7 +2,7 @@ Description=Backup to USB when mounted to {{ backup_to_usb_mount }} Wants={{systemctl_mount_service_name}} After={{systemctl_mount_service_name}} -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service Requires=backups-cleanup.service After=backups-cleanup.service diff --git a/roles/independent_backups-cleanup-service/meta/main.yml b/roles/independent_backups-cleanup-service/meta/main.yml index 7b67d496..efa4997c 100644 --- a/roles/independent_backups-cleanup-service/meta/main.yml +++ b/roles/independent_backups-cleanup-service/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - server_native-python-pip - - independent_systemd-email + - systemd_notifier diff --git a/roles/independent_backups-cleanup-service/templates/backups-cleanup.service.j2 b/roles/independent_backups-cleanup-service/templates/backups-cleanup.service.j2 index f2d91958..96f7cc9b 100644 --- a/roles/independent_backups-cleanup-service/templates/backups-cleanup.service.j2 +++ b/roles/independent_backups-cleanup-service/templates/backups-cleanup.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=delete old backups -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/independent_systemd-email/README.md b/roles/independent_systemd-email/README.md deleted file mode 100644 index ea1c19b0..00000000 --- a/roles/independent_systemd-email/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# server_native-systemd-mail - -# see -- https://wiki.archlinux.org/index.php/Systemd/Timers#MAILTO -- https://serverfault.com/questions/876233/how-to-send-an-email-if-a-systemd-service-is-restarted/876254 -- https://serverfault.com/questions/694818/get-notification-when-systemd-monitored-service-enters-failed-state diff --git a/roles/independent_systemd-email/handlers/main.yml b/roles/independent_systemd-email/handlers/main.yml deleted file mode 100644 index 0b8ff530..00000000 --- a/roles/independent_systemd-email/handlers/main.yml +++ /dev/null @@ -1,4 +0,0 @@ -- name: "restart systemd-email service" - systemd: - name: systemd-email.service - daemon_reload: yes diff --git a/roles/independent_systemd-email/tasks/main.yml b/roles/independent_systemd-email/tasks/main.yml deleted file mode 100644 index 1683d157..00000000 --- a/roles/independent_systemd-email/tasks/main.yml +++ /dev/null @@ -1,18 +0,0 @@ -- name: install smtp-forwarder - pacman: name=smtp-forwarder state=present - -- name: configure msmtprc.conf.j2 - template: src=msmtprc.conf.j2 dest=/root/.msmtprc - -- name: "create {{systemd_email_folder}}" - file: - path: "{{systemd_email_folder}}" - state: directory - mode: 0755 - -- name: configure systemd-email.sh - template: src=systemd-email.sh.j2 dest={{systemd_email_folder}}systemd-email.sh - -- name: configure systemd-email.service - template: src=systemd-email@.service.j2 dest=/etc/systemd/system/systemd-email@.service - notify: restart systemd-email service diff --git a/roles/independent_systemd-email/vars/main.yml b/roles/independent_systemd-email/vars/main.yml deleted file mode 100644 index 8ea81977..00000000 --- a/roles/independent_systemd-email/vars/main.yml +++ /dev/null @@ -1 +0,0 @@ -systemd_email_folder: "{{path_administrator_scripts}}systemd_email/" \ No newline at end of file diff --git a/roles/pc_application-msi-keyboard-color/meta/main.yml b/roles/pc_application-msi-keyboard-color/meta/main.yml index 88c0f0c6..f4d06f73 100644 --- a/roles/pc_application-msi-keyboard-color/meta/main.yml +++ b/roles/pc_application-msi-keyboard-color/meta/main.yml @@ -7,5 +7,6 @@ galaxy_info: - name: Linux versions: - all - dependencies: - - pc_system-aur-helper +dependencies: + - pc_system-aur-helper + - systemd_notifier diff --git a/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 b/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 index 8a6b62c0..c8390410 100644 --- a/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 +++ b/roles/pc_application-msi-keyboard-color/templates/keyboard-color.service.j2 @@ -1,5 +1,6 @@ [Unit] Description=Keyboard Color Service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/pc_system-swapfile/tasks/main.yml b/roles/pc_system-swapfile/tasks/main.yml index 4bf046b0..ac3421fc 100644 --- a/roles/pc_system-swapfile/tasks/main.yml +++ b/roles/pc_system-swapfile/tasks/main.yml @@ -1,6 +1,6 @@ - name: pull swapfile creating script git: - repo: "git@github.com:kevinveenbirkenbach/create-linux-swapfile.git" + repo: "https://github.com/kevinveenbirkenbach/create-linux-swapfile.git" dest: "$HOME/Repositories/github.com/kevinveenbirkenbach/create-linux-swapfile" update: yes become: false diff --git a/roles/server_docker-mailu/meta/main.yml b/roles/server_docker-mailu/meta/main.yml index 0815269b..0b56f345 100644 --- a/roles/server_docker-mailu/meta/main.yml +++ b/roles/server_docker-mailu/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - server_native-docker-reverse-proxy -- independent_systemd-email +- systemd_notifier diff --git a/roles/server_docker-mailu/templates/deploy-letsencrypt-mailu.service.j2 b/roles/server_docker-mailu/templates/deploy-letsencrypt-mailu.service.j2 index c5f00e25..cc2e823e 100644 --- a/roles/server_docker-mailu/templates/deploy-letsencrypt-mailu.service.j2 +++ b/roles/server_docker-mailu/templates/deploy-letsencrypt-mailu.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=Let's Encrypt Mailu Deploy -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-backups-consumer/meta/main.yml b/roles/server_native-backups-consumer/meta/main.yml index 621de431..ed487088 100644 --- a/roles/server_native-backups-consumer/meta/main.yml +++ b/roles/server_native-backups-consumer/meta/main.yml @@ -1,4 +1,4 @@ dependencies: - server_native-git - - independent_systemd-email + - systemd_notifier - independent_backups-cleanup-timer diff --git a/roles/server_native-backups-consumer/templates/pull-remote-backups.service.j2 b/roles/server_native-backups-consumer/templates/pull-remote-backups.service.j2 index 8627f9f0..e64a86d4 100644 --- a/roles/server_native-backups-consumer/templates/pull-remote-backups.service.j2 +++ b/roles/server_native-backups-consumer/templates/pull-remote-backups.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=pull remote backups -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-btrfs-health-check/meta/main.yml b/roles/server_native-btrfs-health-check/meta/main.yml index cd9e921a..b001f0d8 100644 --- a/roles/server_native-btrfs-health-check/meta/main.yml +++ b/roles/server_native-btrfs-health-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - independent_systemd-email + - systemd_notifier diff --git a/roles/server_native-btrfs-health-check/templates/btrfs-health-check.service.j2 b/roles/server_native-btrfs-health-check/templates/btrfs-health-check.service.j2 index 89a73ca3..f5771a81 100644 --- a/roles/server_native-btrfs-health-check/templates/btrfs-health-check.service.j2 +++ b/roles/server_native-btrfs-health-check/templates/btrfs-health-check.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=Check btrfs status -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-certbot-nginx/meta/main.yml b/roles/server_native-certbot-nginx/meta/main.yml index b48ccb6c..5dc331ef 100644 --- a/roles/server_native-certbot-nginx/meta/main.yml +++ b/roles/server_native-certbot-nginx/meta/main.yml @@ -1,3 +1,3 @@ dependencies: - server_native-nginx -- independent_systemd-email +- systemd_notifier diff --git a/roles/server_native-certbot-nginx/templates/certbot.service.j2 b/roles/server_native-certbot-nginx/templates/certbot.service.j2 index a4bceada..a9c46167 100644 --- a/roles/server_native-certbot-nginx/templates/certbot.service.j2 +++ b/roles/server_native-certbot-nginx/templates/certbot.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=Let's Encrypt renewal -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-disc-space-check/meta/main.yml b/roles/server_native-disc-space-check/meta/main.yml index cd9e921a..b001f0d8 100644 --- a/roles/server_native-disc-space-check/meta/main.yml +++ b/roles/server_native-disc-space-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - independent_systemd-email + - systemd_notifier diff --git a/roles/server_native-disc-space-check/templates/disc-space-check.service.j2 b/roles/server_native-disc-space-check/templates/disc-space-check.service.j2 index d243c9d3..e90943ad 100644 --- a/roles/server_native-disc-space-check/templates/disc-space-check.service.j2 +++ b/roles/server_native-disc-space-check/templates/disc-space-check.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=checking disc space -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-docker-compose-restart-unhealthy/templates/docker-compose-restart-unhealthy.service.j2 b/roles/server_native-docker-compose-restart-unhealthy/templates/docker-compose-restart-unhealthy.service.j2 index 0fd50a8d..3cc34358 100644 --- a/roles/server_native-docker-compose-restart-unhealthy/templates/docker-compose-restart-unhealthy.service.j2 +++ b/roles/server_native-docker-compose-restart-unhealthy/templates/docker-compose-restart-unhealthy.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=restart unhealthy docker containers -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-docker-health-check/meta/main.yml b/roles/server_native-docker-health-check/meta/main.yml index cd9e921a..b001f0d8 100644 --- a/roles/server_native-docker-health-check/meta/main.yml +++ b/roles/server_native-docker-health-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - independent_systemd-email + - systemd_notifier diff --git a/roles/server_native-docker-health-check/templates/docker-health-check.service.j2 b/roles/server_native-docker-health-check/templates/docker-health-check.service.j2 index 0e921b32..456b36f2 100644 --- a/roles/server_native-docker-health-check/templates/docker-health-check.service.j2 +++ b/roles/server_native-docker-health-check/templates/docker-health-check.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=Checking docker health -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-docker-volume-backup/meta/main.yml b/roles/server_native-docker-volume-backup/meta/main.yml index a4134a9c..5926c6a4 100644 --- a/roles/server_native-docker-volume-backup/meta/main.yml +++ b/roles/server_native-docker-volume-backup/meta/main.yml @@ -1,4 +1,4 @@ dependencies: - server_native-git - server_native-backups-provider - - independent_systemd-email + - systemd_notifier diff --git a/roles/server_native-docker-volume-backup/templates/docker-volume-backup.service.j2 b/roles/server_native-docker-volume-backup/templates/docker-volume-backup.service.j2 index 5f73093e..342207ea 100644 --- a/roles/server_native-docker-volume-backup/templates/docker-volume-backup.service.j2 +++ b/roles/server_native-docker-volume-backup/templates/docker-volume-backup.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=docker volume backup -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-free-disc-space/meta/main.yml b/roles/server_native-free-disc-space/meta/main.yml index cd9e921a..b001f0d8 100644 --- a/roles/server_native-free-disc-space/meta/main.yml +++ b/roles/server_native-free-disc-space/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - independent_systemd-email + - systemd_notifier diff --git a/roles/server_native-free-disc-space/templates/free-disc-space.service.j2 b/roles/server_native-free-disc-space/templates/free-disc-space.service.j2 index bb1a5069..62c17e59 100644 --- a/roles/server_native-free-disc-space/templates/free-disc-space.service.j2 +++ b/roles/server_native-free-disc-space/templates/free-disc-space.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=freeing disc space -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/server_native-journalctl-health-check/meta/main.yml b/roles/server_native-journalctl-health-check/meta/main.yml index cd9e921a..b001f0d8 100644 --- a/roles/server_native-journalctl-health-check/meta/main.yml +++ b/roles/server_native-journalctl-health-check/meta/main.yml @@ -1,2 +1,2 @@ dependencies: - - independent_systemd-email + - systemd_notifier diff --git a/roles/server_native-journalctl-health-check/templates/journalctl-health-check.service.j2 b/roles/server_native-journalctl-health-check/templates/journalctl-health-check.service.j2 index a18459a0..7f687ea1 100644 --- a/roles/server_native-journalctl-health-check/templates/journalctl-health-check.service.j2 +++ b/roles/server_native-journalctl-health-check/templates/journalctl-health-check.service.j2 @@ -1,6 +1,6 @@ [Unit] Description=checking journalctl health -OnFailure=systemd-email@%n.service +OnFailure=systemd-notifier@%n.service [Service] Type=oneshot diff --git a/roles/systemd_notifier/README.md b/roles/systemd_notifier/README.md new file mode 100644 index 00000000..132bcf33 --- /dev/null +++ b/roles/systemd_notifier/README.md @@ -0,0 +1,37 @@ +# systemd_notifier Ansible Role + +Author: Kevin Veen-Birkenbach (kevin@veen.world) + +## Description + +This Ansible role installs a systemd service that sends notifications via both `systemd_notifier_telegram` and `systemd_notifier_email` when any service fails. + +Features include: + +- Configuration of a systemd service for notification. +- Dependency management for the `systemd_notifier_telegram` and `systemd_notifier_email` roles. + +This role was created as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba). + +## License + +This Ansible role is licensed under the AGPL v3 License. See the LICENSE file for the full license text. + +## Role Dependencies + +This role has the following dependencies: + +- `systemd_notifier_telegram` +- `systemd_notifier_email` + +## Features + +- Installs a systemd service for sending notifications. +- Integrates with both Telegram and Email for notification delivery. + +## Contact Information + +For any questions or feedback, please contact the author: + +Author: Kevin Veen-Birkenbach +Email: kevin@veen.world \ No newline at end of file diff --git a/roles/systemd_notifier/handlers/main.yml b/roles/systemd_notifier/handlers/main.yml new file mode 100644 index 00000000..afd16509 --- /dev/null +++ b/roles/systemd_notifier/handlers/main.yml @@ -0,0 +1,4 @@ +- name: "restart systemd-notifier service" + systemd: + name: systemd-notifier.service + daemon_reload: yes diff --git a/roles/systemd_notifier/meta/main.yml b/roles/systemd_notifier/meta/main.yml new file mode 100644 index 00000000..2399c811 --- /dev/null +++ b/roles/systemd_notifier/meta/main.yml @@ -0,0 +1,4 @@ +--- +dependencies: + - role: systemd_notifier_telegram + - role: systemd_notifier_email diff --git a/roles/systemd_notifier/tasks/main.yml b/roles/systemd_notifier/tasks/main.yml new file mode 100644 index 00000000..0cd3b21d --- /dev/null +++ b/roles/systemd_notifier/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: configure systemd-notifier@.service + template: + src: systemd-notifier@.service.j2 + dest: "/etc/systemd/system/systemd-notifier@.service" + notify: "restart systemd-notifier service" diff --git a/roles/systemd_notifier/templates/systemd-notifier@.service.j2 b/roles/systemd_notifier/templates/systemd-notifier@.service.j2 new file mode 100644 index 00000000..ee48fbf8 --- /dev/null +++ b/roles/systemd_notifier/templates/systemd-notifier@.service.j2 @@ -0,0 +1,8 @@ +[Unit] +Description=Notifier for %i + +[Service] +Type=oneshot +ExecStart=/usr/bin/systemctl start systemd_notifier_telegram@%i.service systemd_notifier_email@%i.service +User=root +Group=systemd-journal diff --git a/roles/systemd_notifier_email/README.md b/roles/systemd_notifier_email/README.md new file mode 100644 index 00000000..c9d48ecf --- /dev/null +++ b/roles/systemd_notifier_email/README.md @@ -0,0 +1,44 @@ +# systemd_notifier_email Ansible Role + +Author: Kevin Veen-Birkenbach (kevin@veen.world) + +## Description + +This Ansible role installs the necessary components for sending email notifications through systemd when any service fails. It configures the `systemd_notifier_email` service and handles the setup of email parameters and templates. + +Features include: + +- Installation and configuration of an email sending service. +- Customizable email templates for service failure notifications. + +This role is part of the `systemd_notifier` suite, which provides a comprehensive solution for service failure notifications in a systemd environment. + +This role was created as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba). + +## Requirements + +This role has the following requirements: + +- Access to an SMTP server for sending email notifications. +- Availability of the `msmtp` package on the target system. + +## Role Variables + +The following variables can be customized in the role's `vars/main.yml` file: + +- `systemd_notifier_email_folder`: The path to the folder where email-related scripts and configurations will be stored. + +## Usage + +To use this role, include it as a dependency in your playbook or role that requires email notifications. Ensure that the `systemd_notifier_email` role is correctly referenced and configured. + +## License + +This Ansible role is licensed under the AGPL v3 License. See the LICENSE file for the full license text. + +## Contact Information + +For any questions or feedback, please contact the author: + +Author: Kevin Veen-Birkenbach +Email: kevin@veen.world \ No newline at end of file diff --git a/roles/systemd_notifier_email/handlers/main.yml b/roles/systemd_notifier_email/handlers/main.yml new file mode 100644 index 00000000..a9d7c24a --- /dev/null +++ b/roles/systemd_notifier_email/handlers/main.yml @@ -0,0 +1,4 @@ +- name: "restart systemd_notifier_email service" + systemd: + name: systemd_notifier_email.service + daemon_reload: yes diff --git a/roles/systemd_notifier_email/tasks/main.yml b/roles/systemd_notifier_email/tasks/main.yml new file mode 100644 index 00000000..62c067e0 --- /dev/null +++ b/roles/systemd_notifier_email/tasks/main.yml @@ -0,0 +1,18 @@ +- name: install smtp-forwarder + pacman: name=smtp-forwarder state=present + +- name: configure msmtprc.conf.j2 + template: src=msmtprc.conf.j2 dest=/root/.msmtprc + +- name: "create {{systemd_notifier_email_folder}}" + file: + path: "{{systemd_notifier_email_folder}}" + state: directory + mode: 0755 + +- name: configure systemd_notifier_email.sh + template: src=systemd_notifier_email.sh.j2 dest={{systemd_notifier_email_folder}}systemd_notifier_email.sh + +- name: configure systemd_notifier_email.service + template: src=systemd_notifier_email@.service.j2 dest=/etc/systemd/system/systemd_notifier_email@.service + notify: restart systemd_notifier_email service diff --git a/roles/independent_systemd-email/templates/msmtprc.conf.j2 b/roles/systemd_notifier_email/templates/msmtprc.conf.j2 similarity index 100% rename from roles/independent_systemd-email/templates/msmtprc.conf.j2 rename to roles/systemd_notifier_email/templates/msmtprc.conf.j2 diff --git a/roles/independent_systemd-email/templates/systemd-email.sh.j2 b/roles/systemd_notifier_email/templates/systemd_notifier_email.sh.j2 similarity index 100% rename from roles/independent_systemd-email/templates/systemd-email.sh.j2 rename to roles/systemd_notifier_email/templates/systemd_notifier_email.sh.j2 diff --git a/roles/independent_systemd-email/templates/systemd-email@.service.j2 b/roles/systemd_notifier_email/templates/systemd_notifier_email@.service.j2 similarity index 55% rename from roles/independent_systemd-email/templates/systemd-email@.service.j2 rename to roles/systemd_notifier_email/templates/systemd_notifier_email@.service.j2 index 86f537e9..7d323698 100644 --- a/roles/independent_systemd-email/templates/systemd-email@.service.j2 +++ b/roles/systemd_notifier_email/templates/systemd_notifier_email@.service.j2 @@ -3,6 +3,6 @@ Description=status email for %i to user [Service] Type=oneshot -ExecStart=/bin/bash {{systemd_email_folder}}systemd-email.sh %i +ExecStart=/bin/bash {{systemd_notifier_email_folder}}systemd_notifier_email.sh %i User=root Group=systemd-journal diff --git a/roles/systemd_notifier_email/vars/main.yml b/roles/systemd_notifier_email/vars/main.yml new file mode 100644 index 00000000..48833fdc --- /dev/null +++ b/roles/systemd_notifier_email/vars/main.yml @@ -0,0 +1 @@ +systemd_notifier_email_folder: "{{path_administrator_scripts}}systemd_notifier_email/" \ No newline at end of file diff --git a/roles/systemd_notifier_telegram/README.md b/roles/systemd_notifier_telegram/README.md new file mode 100644 index 00000000..21b24159 --- /dev/null +++ b/roles/systemd_notifier_telegram/README.md @@ -0,0 +1,41 @@ +## Description + +This Ansible role installs the necessary components for sending Telegram notifications through systemd when any service fails. It configures the `systemd_notifier_telegram` service and handles the setup of Telegram bot parameters and notification templates. + +Features include: + +- Installation and configuration of a Telegram notifier service. +- Customizable templates for Telegram messages sent on service failure. + +This role is part of the `systemd_notifier` suite, which provides a comprehensive solution for service failure notifications in a systemd environment. + +## Requirements + +This role has the following requirements: + +- Access to a Telegram bot token for sending messages. +- Availability of the `curl` package on the target system. + +## Role Variables + +The following variables can be customized in the role's `vars/main.yml` file: + +- `systemd_notifier_telegram_folder`: The path to the folder where Telegram-related scripts and configurations will be stored. +- `systemd_notifier_telegram_script`: The full path to the systemd-telegram.sh script. + +This role was created as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba). + +## Usage + +To use this role, include it as a dependency in your playbook or role that requires Telegram notifications. Ensure that the `systemd_notifier_telegram` role is correctly referenced and configured. + +## License + +This Ansible role is licensed under the AGPL v3 License. See the LICENSE file for the full license text. + +## Contact Information + +For any questions or feedback, please contact the author: + +Author: Kevin Veen-Birkenbach +Email: kevin@veen.world \ No newline at end of file diff --git a/roles/systemd_notifier_telegram/handlers/main.yml b/roles/systemd_notifier_telegram/handlers/main.yml new file mode 100644 index 00000000..d8cf8272 --- /dev/null +++ b/roles/systemd_notifier_telegram/handlers/main.yml @@ -0,0 +1,4 @@ +- name: "restart systemd_notifier_telegram service" + systemd: + name: systemd_notifier_telegram.service + daemon_reload: yes diff --git a/roles/systemd_notifier_telegram/tasks/main.yml b/roles/systemd_notifier_telegram/tasks/main.yml new file mode 100644 index 00000000..0e05ac6c --- /dev/null +++ b/roles/systemd_notifier_telegram/tasks/main.yml @@ -0,0 +1,21 @@ +- name: install curl + pacman: + name: curl + state: present + +- name: Create a directory with a subdirectory + ansible.builtin.file: + path: "{{systemd_telegram_folder}}" + state: directory + mode: '0755' + +- name: configure systemd_notifier_telegram.sh + template: + src: systemd_notifier_telegram.sh.j2 + dest: "{{ systemd_telegram_script }}" + +- name: configure systemd_notifier_telegram.service + template: + src: systemd_notifier_telegram@.service.j2 + dest: "/etc/systemd/system/systemd_notifier_telegram@.service" + notify: "restart systemd_notifier_telegram service" diff --git a/roles/systemd_notifier_telegram/templates/systemd_notifier_telegram.sh.j2 b/roles/systemd_notifier_telegram/templates/systemd_notifier_telegram.sh.j2 new file mode 100644 index 00000000..ff1ecd3a --- /dev/null +++ b/roles/systemd_notifier_telegram/templates/systemd_notifier_telegram.sh.j2 @@ -0,0 +1,2 @@ +#!/bin/bash +/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 failed" diff --git a/roles/systemd_notifier_telegram/templates/systemd_notifier_telegram@.service.j2 b/roles/systemd_notifier_telegram/templates/systemd_notifier_telegram@.service.j2 new file mode 100644 index 00000000..d2f9b49f --- /dev/null +++ b/roles/systemd_notifier_telegram/templates/systemd_notifier_telegram@.service.j2 @@ -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 diff --git a/roles/systemd_notifier_telegram/vars/main.yml b/roles/systemd_notifier_telegram/vars/main.yml new file mode 100644 index 00000000..f958ecec --- /dev/null +++ b/roles/systemd_notifier_telegram/vars/main.yml @@ -0,0 +1,2 @@ +systemd_telegram_folder: "/opt/ansible-roles/systemd_notifier_telegram/" +systemd_telegram_script: "{{systemd_telegram_folder}}systemd_notifier_telegram.sh"