From 460a35e82eb9e26769cb648cd2555f0c6ac2f01b Mon Sep 17 00:00:00 2001 From: transcaffeine Date: Sat, 8 Jun 2024 00:20:02 +0200 Subject: [PATCH] feat(vmtsdb): add role for deployment with docker --- README.md | 2 ++ playbooks/vmtsdb.yml | 6 +++++ roles/vmtsdb/README.md | 7 +++++ roles/vmtsdb/defaults/main.yml | 45 ++++++++++++++++++++++++++++++++ roles/vmtsdb/tasks/main.yml | 47 ++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 playbooks/vmtsdb.yml create mode 100644 roles/vmtsdb/README.md create mode 100644 roles/vmtsdb/defaults/main.yml create mode 100644 roles/vmtsdb/tasks/main.yml diff --git a/README.md b/README.md index c41ff9e..de7a924 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ metrics or alerting. manager receiver which posts alerts to a configured matrix channel using alertmanagers' webhooks. +- [`vmtsdb`](roles/vmtsdb/README.md): VictoriaMetrics time series database. + - [`postgres_exporter`](roles/postgres_exporter/README.md): Prometheus exporter for postgres databases, in a docker container. diff --git a/playbooks/vmtsdb.yml b/playbooks/vmtsdb.yml new file mode 100644 index 0000000..953a420 --- /dev/null +++ b/playbooks/vmtsdb.yml @@ -0,0 +1,6 @@ +--- +- name: Install vmtsdb using docker + hosts: "{{ vmtsdb_hosts | default('vmtsdb') }}" + become: "{{ vmtsdb_become | default(false) }}" + roles: + - role: finallycoffee.observability.vmtsdb diff --git a/roles/vmtsdb/README.md b/roles/vmtsdb/README.md new file mode 100644 index 0000000..d4db5fa --- /dev/null +++ b/roles/vmtsdb/README.md @@ -0,0 +1,7 @@ +# `finallycoffee.observability.vmtsdb` ansible role + +## Description + +This role configures `vmtsdb`, the time-series database part of victoria metrics, run in a docker container. + +Per default `enableTCP6` and `envflag.enable` flags are passed to victoriametrics, enabling configuration using `vmtsdb_container_env`, using the syntax found on the official victoriametrics documentation. diff --git a/roles/vmtsdb/defaults/main.yml b/roles/vmtsdb/defaults/main.yml new file mode 100644 index 0000000..d309958 --- /dev/null +++ b/roles/vmtsdb/defaults/main.yml @@ -0,0 +1,45 @@ +--- +vmtsdb_state: present +vmtsdb_user: vmtsdb +vmtsdb_version: "1.87.5" +vmtsdb_base_path: "/opt/vmtsdb" +vmtsdb_data_path: "{{ vmtsdb_base_path }}/data" + +vmtsdb_container_image_server: docker.io +vmtsdb_container_image_namespace: "victoriametrics" +vmtsdb_container_image_container: "victoria-metrics" +vmtsdb_container_image_name: >-2 + {{ + vmtsdb_container_image_server + + ((vmtsdb_container_image_namespace is defined) + | ternary('/' ~ vmtsdb_container_image_namespace, '')) + + '/' + vmtsdb_container_image_container + }} +#vmtsdb_container_image_tag: +vmtsdb_container_image: >-2 + {{ vmtsdb_container_image_name }}:{{ vmtsdb_container_image_tag | default('v' + vmtsdb_version, false) }} + +vmtsdb_user_id: >- + {{ (vmtsdb_user_info is defined and 'uid' in vmtsdb_user_info) | ternary(vmtsdb_user_info.uid, vmtsdb_user) }} +vmtsdb_group_id: >- + {{ (vmtsdb_user_info is defined and 'group' in vmtsdb_user_info) | ternary(vmtsdb_user_info.group, vmtsdb_user) }} +vmtsdb_container_user: "{{ vmtsdb_user_id }}" +vmtsdb_container_group: "{{ vmtsdb_group_id }}" +vmtsdb_container_name: "vmtsdb" +vmtsdb_container_command: [] +vmtsdb_container_default_command: + - "-enableTCP6" + - "-envflag.enable" +vmtsdb_container_merged_command: >- + {{ vmtsdb_container_default_command + (vmtsdb_container_command | default([], false)) }} +vmtsdb_container_env: {} +vmtsdb_container_default_env: + PATH: "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" +vmtsdb_container_merged_env: >- + {{ vmtsdb_container_default_env | combine(vmtsdb_container_env) }} +vmtsdb_container_volumes: [] +vmtsdb_container_default_volumes: + - "{{ vmtsdb_data_path }}:/victoria-metrics-data:z" +vmtsdb_container_merged_volumes: >- + {{ vmtsdb_container_default_volumes | combine(vmtsdb_container_volumes) }} +vmtsdb_container_restart_policy: "unless-stopped" diff --git a/roles/vmtsdb/tasks/main.yml b/roles/vmtsdb/tasks/main.yml new file mode 100644 index 0000000..7365f0d --- /dev/null +++ b/roles/vmtsdb/tasks/main.yml @@ -0,0 +1,47 @@ +--- +- name: Ensure user {{ vmtsdb_user }} is {{ vmtsdb_state }} + ansible.builtin.user: + name: "{{ vmtsdb_user }}" + state: present + system: true + create_home: false + register: vmtsdb_user_info + +- name: Ensure directories for vmtsdb are {{ vmtsdb_state }} + ansible.builtin.file: + path: "{{ item.path }}" + state: "{{ (vmtsdb_state == 'present') | ternary('directory', 'absent') }}" + owner: "{{ item.owner | default(vmtsdb_user_id) }}" + group: "{{ item.group | default(vmtsdb_group_id) }}" + mode: "{{ item.mode | default('0775') }}" + loop: + - path: "{{ vmtsdb_base_path }}" + - path: "{{ vmtsdb_data_path }}" + mode: "0755" + loop_control: + label: "{{ item.path }}" + +- name: Ensure container image {{ vmtsdb_container_image }} is {{ vmtsdb_state }} + community.docker.docker_image: + name: "{{ vmtsdb_container_image }}" + state: "{{ vmtsdb_state }}" + source: "{{ (vmtsdb_state == 'present') | ternary('pull', omit) }}" + force_source: >-2 + {{ (vmtsdb_container_image == 'present') | ternary(vmtsdb_container_image_tag, omit) }} + +- name: Ensure vmtsdb container is {{ vmtsdb_state }} + community.docker.docker_container: + name: "{{ vmtsdb_container_name}}" + image: "{{ vmtsdb_container_image }}" + env: "{{ vmtsdb_container_merged_env }}" + user: "{{ vmtsdb_container_user }}" + ports: "{{ vmtsdb_container_ports | default(omit) }}" + groups: "{{ vmtsdb_container_group }}" + labels: "{{ vmtsdb_container_labels | default(omit) }}" + volumes: "{{ vmtsdb_container_merged_volumes }}" + command: "{{ vmtsdb_container_merged_command }}" + networks: "{{ vmtsdb_container_networks | default(omit) }}" + etc_hosts: "{{ vmtsdb_container_etc_hosts | default(omit )}}" + purge_networks: "{{ vmtsdb_container_purge_networks | default(omit) }}" + restart_policy: "{{ vmtsdb_container_restart_policy | default(omit) }}" + state: "{{ (vmtsdb_state == 'present') | ternary('started', 'absent') }}"