From e173fab4e96540a32cfa5a2c86f6274f2384ff14 Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Sun, 13 Mar 2022 08:18:22 +0100 Subject: [PATCH] feat(matrix-alertmanager): add ansible role for deployment using docker --- roles/matrix-alertmanager/README.md | 20 +++++++++ roles/matrix-alertmanager/defaults/main.yml | 39 +++++++++++++++++ roles/matrix-alertmanager/tasks/main.yml | 46 +++++++++++++++++++++ roles/matrix-alertmanager/templates/env.j2 | 7 ++++ roles/matrix-alertmanager/vars/main.yml | 16 +++++++ 5 files changed, 128 insertions(+) create mode 100644 roles/matrix-alertmanager/README.md create mode 100644 roles/matrix-alertmanager/defaults/main.yml create mode 100644 roles/matrix-alertmanager/tasks/main.yml create mode 100644 roles/matrix-alertmanager/templates/env.j2 create mode 100644 roles/matrix-alertmanager/vars/main.yml diff --git a/roles/matrix-alertmanager/README.md b/roles/matrix-alertmanager/README.md new file mode 100644 index 0000000..76a6350 --- /dev/null +++ b/roles/matrix-alertmanager/README.md @@ -0,0 +1,20 @@ +# `finallycoffee.observability.matrix-alertmanager` ansible role + +## Overview + +Runs [matrix-alertmanager](https://github.com/jaywink/matrix-alertmanager) +in a docker container, and bridges alerts from alertmanager +into a configured matrix room (per configured receiver). + +## Configuration + +### Required configuration + +The following variables need to be defined in order for `matrix-alertmanager` to +be able to work: + +- `matrix_alertmanager_secret`: The secret configured in alertmanager for this receiver +- `matrix_alertmanager_homeserver_url`: URL to the homeserver to log in to, including scheme and port. +- `matrix_alertmanager_mxid`: The matrix ID in the form `@local:server.tld` to use +- `matrix_alertmanager_access_token`: The matrix access token for `matrix_alertmanager_mxid` (Note: this is not the password) +- `matrix_alertmanager_rooms`: A list of objects `{ name, room_id }` where `name` is the receiver name in alertmanager and `room_id` is a matrix room ID (not an alias) diff --git a/roles/matrix-alertmanager/defaults/main.yml b/roles/matrix-alertmanager/defaults/main.yml new file mode 100644 index 0000000..35136cf --- /dev/null +++ b/roles/matrix-alertmanager/defaults/main.yml @@ -0,0 +1,39 @@ +--- + +matrix_alertmanager_prefix: "" +matrix_alertmanager_user: "mxalerts" +matrix_alertmanager_base_path: "/opt/matrix-alerts" +matrix_alertmanager_config_file_path: "{{ matrix_alertmanager_base_path }}/env" + +matrix_alertmanager_listen_port: 3000 +matrix_alertmanager_secret: ~ +matrix_alertmanager_homeserver_url: ~ +matrix_alertmanager_rooms: [] +matrix_alertmanager_mxid: ~ +matrix_alertmanager_access_token: ~ +matrix_alertmanager_mention_room: false + +matrix_alertmanager_container_name: matrix-alerts +matrix_alertmanager_container_image_name: "docker.io/jaywink/matrix-alertmanager" +matrix_alertmanager_container_image_tag: latest +matrix_alertmanager_container_image: >-2 + {{ matrix_alertmanager_container_image_name + ':' + matrix_alertmanager_container_image_tag }} +matrix_alertmanager_container_env: >-2 + {{ matrix_alertmanager_container_base_env | combine(matrix_alertmanager_container_extra_env }} +matrix_alertmanager_container_extra_env: {} +matrix_alertmanager_container_ports: >-2 + {{ matrix_alertmanager_container_base_ports + matrix_alertmanager_container_extra_ports }} +matrix_alertmanager_container_extra_ports: [] +matrix_alertmanager_container_networks: [] +matrix_alertmanager_container_purge_networks: false +matrix_alertmanager_container_volumes: >-2 + {{ matrix_alertmanager_container_base_volumes + matrix_alertmanager_container_extra_volumes }} +matrix_alertmanager_container_extra_volumes: [] +matrix_alertmanager_container_labels: >-2 + {{ matrix_alertmanager_container_base_labels | combine(matrix_alertmanager_container_extra_labels) }} +matrix_alertmanager_container_extra_labels: {} +matrix_alertmanager_container_capabilities: ~ +matrix_alertmanager_container_etc_hosts: ~ +matrix_alertmanager_container_restart_policy: unless-stopped +#matrix_alertmanager_ +#matrix_alertmanager_ diff --git a/roles/matrix-alertmanager/tasks/main.yml b/roles/matrix-alertmanager/tasks/main.yml new file mode 100644 index 0000000..214c6e5 --- /dev/null +++ b/roles/matrix-alertmanager/tasks/main.yml @@ -0,0 +1,46 @@ +--- + +- name: Ensure user '{{ matrix_alertmanager_user }}' is created + user: + name: "{{ matrix_alertmanager_user }}" + state: present + system: yes + when: matrix_alertmanager_user is string and matrix_alertmanager_user is not 'root' + register: matrix_alertmanager_user_info + +- name: Ensure base directory '{{ matrix_alertmanager_base_path }}' exists + file: + path: "{{ matrix_alertmanager_base_path }}" + state: directory + owner: "{{ matrix_alertmanager_run_user }}" + group: "{{ matrix_alertmanager_run_group }}" + mode: "0750" + +- name: Ensure config file '{{ matrix_alertmanager_config_file_path }}' is templated + template: + src: env.j2 + dest: "{{ matrix_alertmanager_base_path }}" + owner: "{{ matrix_alertmanager_run_user }}" + group: "{{ matrix_alertmanager_run_group }}" + mode: "0640" + vars: + matrix_alertmanager_rooms_flattened: >-2 + {%- for receiver in matrix_alertmanager_rooms_flattened -%} + {{ receiver.name }}/{{ receiver.room_id }}{{ '' if loop.last else '|' }} + {%- endfor %} + +- name: Ensure docker container '{{ matrix_alertmanager_container_name }}' is running + docker_container: + name: "{{ matrix_alertmanager_container_name }}" + image: "{{ matrix_alertmanager_container_image }}" + env: "{{ matrix_alertmanager_container_env }}" + user: "{{ matrix_alertmanager_run_user }}:{{ matrix_alertmanager_run_group }}" + ports: "{{ matrix_alertmanager_container_ports }}" + labels: "{{ matrix_alertmanager_container_labels }}" + volumes: "{{ matrix_alertmanager_container_volumes }}" + networks: "{{ matrix_alertmanager_container_networks | default(omit, True) }}" + capabilities: "{{ matrix_alertmanager_container_capabilities | default(omit, True) }}" + purge_networks: "{{ matrix_alertmanager_container_purge_networks | default(omit, True) }}" + etc_hosts: "{{ matrix_alertmanager_container_etc_hosts | default(omit) }}" + restart_policy: "{{ matrix_alertmanager_container_restart_policy }}" + state: started diff --git a/roles/matrix-alertmanager/templates/env.j2 b/roles/matrix-alertmanager/templates/env.j2 new file mode 100644 index 0000000..f25e78d --- /dev/null +++ b/roles/matrix-alertmanager/templates/env.j2 @@ -0,0 +1,7 @@ +APP_PORT={{ matrix_alertmanager_listen_port }} +APP_ALERTMANAGER_SECRET={{ matrix_alertmanager_secret }} +MATRIX_HOMESERVER_URL={{ matrix_alertmanager_homeserver_url }} +MATRIX_ROOMS={{ matrix_alertmanager_rooms_flattened }} +MATRIX_USER={{ matrix_alertmanager_mxid }} +MATRIX_TOKEN={{ matrix_alertmanager_access_token }} +MENTION_ROOM={{ '1' if matrix_alertmanager_mention_room else '0' }} diff --git a/roles/matrix-alertmanager/vars/main.yml b/roles/matrix-alertmanager/vars/main.yml new file mode 100644 index 0000000..6e95357 --- /dev/null +++ b/roles/matrix-alertmanager/vars/main.yml @@ -0,0 +1,16 @@ +--- + +matrix_alertmanager_run_user: >-2 + {{ matrix_alertmanager_user_info.uid + if matrix_alertmanager_user is not 'root' + else matrix_alertmanager_user }} +matrix_alertmanager_run_group: >-2 + {{ matrix_alertmanager_user_info.group + if matrix_alertmanager_user is not 'root' + else matrix_alertmanager_user }} + +matrix_alertmanager_container_base_env: {} +matrix_alertmanager_container_base_ports: [] +matrix_alertmanager_container_base_volumes: + - "{{ matrix_alertmanager_config_file_path }}:/app/.env:ro" +matrix_alertmanager_container_base_labels: {}