From 48d940b11776842c68281e7c94e17087de972482 Mon Sep 17 00:00:00 2001 From: Blallo Date: Tue, 23 Feb 2021 18:49:29 +0100 Subject: [PATCH] Init --- handlers/main.yml | 4 ++++ tasks/main.yml | 35 ++++++++++++++++++++++++++++ templates/notify-failure@.service.j2 | 9 +++++++ templates/notify_unit_failure.sh | 16 +++++++++++++ templates/sendmail.toml.j2 | 17 ++++++++++++++ 5 files changed, 81 insertions(+) create mode 100644 handlers/main.yml create mode 100644 tasks/main.yml create mode 100644 templates/notify-failure@.service.j2 create mode 100755 templates/notify_unit_failure.sh create mode 100644 templates/sendmail.toml.j2 diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..bb7fde2 --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,4 @@ +--- +- name: reload systemd + systemd: + daemon_reload: yes diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..555ed62 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,35 @@ +--- +- name: Ensure sendmail is present + get_url: + url: "https://git.abbiamoundominio.org/blallo/sendmail/releases/download/{{ sendmail.version }}/sendmail" + dest: /usr/local/bin/sendmail + owner: root + group: root + mode: 0755 + +- name: Ensure sendmail template is present + template: + src: templates/sendmail.toml.j2 + dest: /etc/sendmail.toml + owner: root + group: root + mode: 0644 + vars: + - sections: "{{ sendmail.configuration }}" + +- name: Ensure notify_unit_failure.sh is present + template: + src: templates/notify_unit_failure.sh + dest: /usr/local/bin/notify_unit_failure.sh + owner: root + group: root + mode: 0755 + +- name: Ensure notify-failure@.service is present + template: + src: templates/notify-failure@.service.j2 + dest: /etc/systemd/system/notify-failure@.service + owner: root + group: root + mode: 0644 + notify: reload systemd diff --git a/templates/notify-failure@.service.j2 b/templates/notify-failure@.service.j2 new file mode 100644 index 0000000..85f3765 --- /dev/null +++ b/templates/notify-failure@.service.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=notify failure of %i unit via mail + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/notify_unit_failure.sh %i + + +# vim: set ft=dosini: diff --git a/templates/notify_unit_failure.sh b/templates/notify_unit_failure.sh new file mode 100755 index 0000000..45472fe --- /dev/null +++ b/templates/notify_unit_failure.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +INVOCATION_ID=$(systemctl show -p InvocationID --value ${1}.service) + +cat << EOM | sendmail \ + -section unit_failure \ + -sub "[UNIT-FAILURE|$(hostname)] ${1}" +The unit ${1}.service just failed +Date: $(date) + +--- +$(journalctl _SYSTEMD_INVOCATION_ID=${INVOCATION_ID}) + + + +EOM diff --git a/templates/sendmail.toml.j2 b/templates/sendmail.toml.j2 new file mode 100644 index 0000000..effa361 --- /dev/null +++ b/templates/sendmail.toml.j2 @@ -0,0 +1,17 @@ +{% for section in sections %} +[{{ section.name }}] +{% if section.from is defined %} + from = "{{ section.from }}" +{% endif %} +{% if section.to_list is defined %} + to = {{ section.to_list }} +{% endif %} + [{{ section.name }}.server] + address = "{{ section.server.address }}" + port = {{ section.server.port }} + user = "{{ section.server.user }}" + password = "{{ section.server.password }}" + +{% endfor %} + +# vim: set ft=toml et sw=0 ts=2 sts=0: