From c0fcf3238d46aff88f44033de72281b5a6f5198a Mon Sep 17 00:00:00 2001 From: Ricard Illa Date: Tue, 9 May 2023 09:56:59 +0200 Subject: [PATCH] feat: notifiers role --- roles/notifiers/files/bin/notify-gotify | 21 ++++++++++++ roles/notifiers/files/bin/notify-xmpp | 12 +++++++ roles/notifiers/tasks/main.yml | 33 +++++++++++++++++++ .../templates/etc/notifiers/gotify.j2 | 2 ++ .../notifiers/templates/etc/notifiers/xmpp.j2 | 3 ++ 5 files changed, 71 insertions(+) create mode 100644 roles/notifiers/files/bin/notify-gotify create mode 100644 roles/notifiers/files/bin/notify-xmpp create mode 100644 roles/notifiers/tasks/main.yml create mode 100644 roles/notifiers/templates/etc/notifiers/gotify.j2 create mode 100644 roles/notifiers/templates/etc/notifiers/xmpp.j2 diff --git a/roles/notifiers/files/bin/notify-gotify b/roles/notifiers/files/bin/notify-gotify new file mode 100644 index 0000000..71f0820 --- /dev/null +++ b/roles/notifiers/files/bin/notify-gotify @@ -0,0 +1,21 @@ +#!/bin/sh + +# shellcheck disable=SC1091 +. /usr/local/etc/notifiers/gotify + +PRIORITY="${PRIORITY:-5}" + +if [ -n "$TITLE" ]; then + TITLE_ARG="--form title=${TITLE}" +else + TITLE_ARG="" +fi + +MSG="$*" + +# shellcheck disable=SC2086 +curl \ + "${GOTIFY_URL}/message?token=${GOTIFY_TOKEN}" \ + $TITLE_ARG \ + --form "message=${MSG}" \ + --form "priority=${PRIORITY}" diff --git a/roles/notifiers/files/bin/notify-xmpp b/roles/notifiers/files/bin/notify-xmpp new file mode 100644 index 0000000..a17a68a --- /dev/null +++ b/roles/notifiers/files/bin/notify-xmpp @@ -0,0 +1,12 @@ +#!/bin/sh + +# shellcheck disable=SC1091 +. /usr/local/etc/notifiers/xmpp + +MSG="$*" + +echo "$MSG" | + go-sendxmpp \ + --username="$XMPP_ACCOUNT" \ + --password="$XMPP_PASSWORD" \ + "$XMPP_RECIPIENT" diff --git a/roles/notifiers/tasks/main.yml b/roles/notifiers/tasks/main.yml new file mode 100644 index 0000000..8215d1e --- /dev/null +++ b/roles/notifiers/tasks/main.yml @@ -0,0 +1,33 @@ +--- + +- name: install notification programs + apk: + name: + - curl + - go-sendxmpp + - msmtp + when: ansible_distribution == "Alpine" + +- name: create config dir + file: + state: directory + path: /usr/local/etc/notifiers + +- name: render notifier configs + template: + src: "etc/notifiers/{{ item }}.j2" + dest: "/usr/local/etc/notifiers/{{ item }}" + owner: root + mode: '0600' + loop: + - gotify + - xmpp + +- name: copy notifier executables + copy: + src: "bin/{{ item }}" + dest: "/usr/local/bin/{{ item }}" + mode: '0755' + loop: + - notify-gotify + - notify-xmpp diff --git a/roles/notifiers/templates/etc/notifiers/gotify.j2 b/roles/notifiers/templates/etc/notifiers/gotify.j2 new file mode 100644 index 0000000..272c0d1 --- /dev/null +++ b/roles/notifiers/templates/etc/notifiers/gotify.j2 @@ -0,0 +1,2 @@ +GOTIFY_URL='{{ notifiers.gotify.url }}' +GOTIFY_TOKEN='{{ notifiers.gotify.token }}' diff --git a/roles/notifiers/templates/etc/notifiers/xmpp.j2 b/roles/notifiers/templates/etc/notifiers/xmpp.j2 new file mode 100644 index 0000000..593e4d5 --- /dev/null +++ b/roles/notifiers/templates/etc/notifiers/xmpp.j2 @@ -0,0 +1,3 @@ +XMPP_RECIPIENT='{{ notifiers.xmpp.recipient }}' +XMPP_ACCOUNT='{{ notifiers.xmpp.account }}' +XMPP_PASSWORD='{{ notifiers.xmpp.password }}'