From fc65595bce678304c023cf61a443bd22f41b1509 Mon Sep 17 00:00:00 2001 From: Johanna Dorothea Reichmann Date: Sun, 13 Mar 2022 08:41:06 +0100 Subject: [PATCH] feat(postgres_exporter): add ansible role for deployment using docker --- README.md | 3 +++ roles/postgres_exporter/README.md | 18 +++++++++++++ roles/postgres_exporter/defaults/main.yml | 31 +++++++++++++++++++++++ roles/postgres_exporter/tasks/main.yml | 30 ++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 roles/postgres_exporter/README.md create mode 100644 roles/postgres_exporter/defaults/main.yml create mode 100644 roles/postgres_exporter/tasks/main.yml diff --git a/README.md b/README.md index aa06751..e6262a5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ metrics or alerting. manager receiver which posts alerts to a configured matrix channel using alertmanagers' webhooks. +- [`postgres_exporter`](roles/postgres_exporter/README.md): Prometheus + exporter for postgres databases, in a docker container. + ## License [CNPLv7+](LICENSE.md): Cooperative Nonviolent Public License diff --git a/roles/postgres_exporter/README.md b/roles/postgres_exporter/README.md new file mode 100644 index 0000000..388afe7 --- /dev/null +++ b/roles/postgres_exporter/README.md @@ -0,0 +1,18 @@ +# `finallycoffee.observability.postgres_exporter` ansible role + +## Overview + +Ansible role to deploy [`postgres_exporter`](https://github.com/prometheus-community/postgres_exporter), +running in a docker container. + +## Configuration + +Set `postgres_exporter_db_[host|post|user|pass]` according to your +databases configuration, and scrape the container on it's port `9187` +(e.g.: `http://{container_ip}:9187/metrics`). + +For more configuration options using environment variables, see the +[official README](https://github.com/prometheus-community/postgres_exporter) +and set the configuration in `postgres_exporter_container_env` to override +the defaults. + diff --git a/roles/postgres_exporter/defaults/main.yml b/roles/postgres_exporter/defaults/main.yml new file mode 100644 index 0000000..a6d92f3 --- /dev/null +++ b/roles/postgres_exporter/defaults/main.yml @@ -0,0 +1,31 @@ +--- + +postgres_exporter_version: "0.10.1" +postgres_exporter_user: root +postgres_exporter_create_user: false + +postgres_exporter_db_host: ~ +postgres_exporter_db_user: postgres +postgres_exporter_db_pass: ~ +postgres_exporter_db_port: ~ +postgres_exporter_db_sslmode: false + +postgres_exporter_container_name: postgres_exporter +postgres_exporter_container_image_name: quay.io/prometheuscommunity/postgres-exporter +postgres_exporter_container_image_tag: ~ +postgres_exporter_container_image_ref: "{{ postgres_exporter_container_image_name }}:{{ postgres_exporter_container_image_tag | default('v' + postgres_exporter_version, True) }}" +postgres_exporter_container_networks: ~ +postgres_exporter_container_purge_networks: false +postgres_exporter_container_volumes: [] +postgres_exporter_container_ports: ~ +postgres_exporter_container_env: + DATA_SOURCE_NAME: >- + user={{ postgres_exporter_db_user }} + host={{ postgres_exporter_db_host }} + sslmode={{ 'enable' if postgres_exporter_db_sslmode else 'disable' }} + {%- if postgres_exporter_db_pass -%} pass={{ postgres_exporter_db_pass }}{% endif -%} + {%- if postgres_exporter_db_port -%} port={{ postgres_exporter_db_port }}{% endif -%} +postgres_exporter_container_labels: + VERSION: "{{ postgres_exporter_version }}" +postgres_exporter_container_user: "{{ postgres_exporter_user if not postgres_exporter_create_user else postgres_exporter_user_info.uid }}" +postgres_exporter_container_restart_policy: unless-stopped diff --git a/roles/postgres_exporter/tasks/main.yml b/roles/postgres_exporter/tasks/main.yml new file mode 100644 index 0000000..7f1c2fe --- /dev/null +++ b/roles/postgres_exporter/tasks/main.yml @@ -0,0 +1,30 @@ +--- + +- name: Ensure user '{{ postgres_exporter_user }}' exists + user: + name: "{{ postgres_exporter_user }}" + state: present + system: yes + register: postgres_exporter_user_info + when: postgres_exporter_create_user + +- name: Ensure container image '{{ postgres_exporter_container_image_ref }}' is available + community.docker.docker_image: + name: "{{ postgres_exporter_container_image_ref }}" + state: present + source: pull + force_source: "{{ 'yes' if postgres_exporter_container_image_tag else 'no' }}" + +- name: Ensure postgres exporter container '{{ postgres_exporter_container_name }}' is running + docker_container: + name: "{{ postgres_exporter_container_name }}" + image: "{{ postgres_exporter_container_image_ref }}" + networks: "{{ postgres_exporter_container_networks | default(omit) }}" + purge_networks: "{{ postgres_exporter_container_purge_networks }}" + volumes: "{{ postgres_exporter_container_volumes | default(omit) }}" + labels: "{{ postgres_exporter_container_labels | default(omit) }}" + ports: "{{ postgres_exporter_container_ports | default(omit) }}" + user: "{{ postgres_exporter_container_user }}" + env: "{{ postgres_exporter_container_env }}" + restart_policy: "{{ postgres_exporter_container_restart_policy }}" + state: started