From bcc788497f2e229e147d6108bc1300c1569aab50 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 --- roles/postgres_exporter/defaults/main.yml | 35 +++++++++++++++++++++++ roles/postgres_exporter/tasks/main.yml | 30 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 roles/postgres_exporter/defaults/main.yml create mode 100644 roles/postgres_exporter/tasks/main.yml diff --git a/roles/postgres_exporter/defaults/main.yml b/roles/postgres_exporter/defaults/main.yml new file mode 100644 index 0000000..9a3c792 --- /dev/null +++ b/roles/postgres_exporter/defaults/main.yml @@ -0,0 +1,35 @@ +--- + +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) }}" +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_info.uid if postgres_exporter_user_info is defined else postgres_exporter_user }}" +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..d589119 --- /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_container_image: + name: "{{ postgres_exporter_container_image_ref }}" + state: present + source: pull + force_source: "{{ postgres_exporter_container_image_tag|bool }}" + +- 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