feat(vmagent): add ansible role with docker support

This commit is contained in:
transcaffeine 2024-10-10 20:30:00 +02:00
parent 7080d5fd24
commit 0165a03a89
Signed by: transcaffeine
GPG Key ID: 03624C433676E465
12 changed files with 235 additions and 0 deletions

View File

@ -18,6 +18,8 @@ metrics or alerting.
manager receiver which posts alerts to a configured matrix channel
using alertmanagers' webhooks.
- [`vmagent`](roles/vmagent/README.md): VictoriaMetrics agent
- [`vmtsdb`](roles/vmtsdb/README.md): VictoriaMetrics time series database.
- [`vmalert`](roles/vmalert/README.md): VictoriaMetrics alerting and

6
playbooks/vmagent.yml Normal file
View File

@ -0,0 +1,6 @@
---
- name: Install and configure vmagent
hosts: "{{ vmagent_hosts | default('vmagent') }}"
become: "{{ vmagent_become | default(false) }}"
roles:
- role: finallycoffee.observability.vmagent

27
roles/vmagent/README.md Normal file
View File

@ -0,0 +1,27 @@
# `finallycoffee.observability.vmagent` ansible role
Install and configure the
[victoriametrics agent `vmagent`](https://docs.victoriametrics.com/vmagent/)
using the [supported deployment types (see `vars/main.yml#L5`)](vars/main.yml#L5).
## Configuration
Set scrape job configuration as complex data in `vmagent_config_scrape_configs`.
To tune the scrape interval, override `vmagent_config_global_scrape_interval`,
or modify / extend `vmagent_config` directly.
### Prometheus remote write api with basic auth
One of the more common methods of sending the collected metrics to a
central prometheus server. Set the following variables to archieve this:
```yaml
vmagent_flags:
remoteWrite_url: https://my.prometheus.instance.example.com/api/v1/write
remoteWrite_basicAuth_username: my_prom_user
remoteWrite_basicAuth_passwordFile: /path/to/password/file.key
```
For the full set of options, see either the
[vmagents' "Advanced usage" documentation](https://docs.victoriametrics.com/vmagent/#advanced-usage)
or run `vmagent -help` for the same output.

View File

@ -0,0 +1,17 @@
---
vmagent_config_global_scrape_interval: "30s"
vmagent_config_global_scrape_timeout: "10s"
vmagent_config_global_external_labels: []
vmagent_config_scrape_configs: []
vmagent_config: ~
vmagent_base_config:
global:
scrape_interval: "{{ vmagent_config_global_scrape_interval }}"
scrape_timeout: "{{ vmagent_config_global_scrape_timeout }}"
external_labels: "{{ vmagent_config_global_external_labels }}"
scrape_configs: "{{ vmagent_config_scrape_configs }}"
vmagent_merged_config: >-2
{{ (vmagent_base_config | default({}, true))
| combine(vmagent_config | default({}, true), recursive=True) }}

View File

@ -0,0 +1,54 @@
---
vmagent_container_image_registry: "docker.io"
vmagent_container_image_namespace: "victoriametrics"
vmagent_container_image_name: "vmagent"
vmagent_container_image_tag: ~
vmagent_container_image: >-2
{{
([
vmagent_container_image_registry,
vmagent_container_image_namespace,
vmagent_container_image_name,
] | join('/'))
+ ':'
+ (vmagent_container_image_tag
| default('v' + vmagent_version, true))
}}
vmagent_container_image_source: pull
vmagent_container_image_force_source: >-2
{{ vmagent_container_image_tag | default(false, true) | bool }}
vmagent_container_image_network_retries: 3
vmagent_container_image_network_delay: 5
vmagent_container_name: vmagent
vmagent_container_user: ~
vmagent_container_ports: ~
vmagent_container_labels: ~
vmagent_container_command: >-2
{% for flag in vmagent_all_flags -%}
-{{ flag }}
{% endfor -%}
vmagent_container_networks: ~
vmagent_container_etc_hosts: ~
vmagent_container_dns_servers: ~
vmagent_container_restart_policy: >-2
{{ (vmagent_deployment_type == 'docker')
| ternary('unless-stopped', 'on-failure') }}
vmagent_container_state: >-2
{{ (vmagent_state == 'present') | ternary('started', 'absent') }}
vmagent_container_base_volumes:
- "{{ vmagent_scrape_config_file }}:{{ vmagent_scrape_config_file }}:ro"
- "{{ vmagent_cache_path }}:{{ vmagent_cache_path }}:z"
vmagent_container_volumes: ~
vmagent_container_all_volumes: >-2
{{ (vmagent_container_base_volumes | default([], true))
+ (vmagent_container_volumes | default([], true)) }}
vmagent_container_base_env:
remoteWrite_tmpDataPath: "{{ vmagent_cache_path }}"
promscrape_config: "{{ vmagent_scrape_config_file }}"
vmagent_container_env: ~
vmagent_container_merged_env: >-2
{{ (vmagent_container_base_env | default({}, true))
| combine(vmagent_container_env | default({})) }}

View File

@ -0,0 +1,16 @@
---
vmagent_user: vmagent
vmagent_version: "1.104.0"
vmagent_state: present
vmagent_deployment_method: "docker"
vmagent_scrape_config_file: "/etc/vmagent/scrape_config.yml"
vmagent_config_path: "{{ vmagent_scrape_config_file | dirname }}"
vmagent_cache_path: "/var/cache/vmagent"
vmagent_base_flags:
- "enableTCP6"
- "envflag.enable"
vmagent_flags: ~
vmagent_all_flags: >-2
{{ vmagent_base_flags + (vmagent_flags | default([], true)) }}

View File

@ -0,0 +1,7 @@
---
vmagent_user_groups: ~
vmagent_run_user_id: >-2
{{ vmagent_user_info.uid | default(vmagent_user) }}
vmagent_run_group_id: >-2
{{ vmagent_user_info.group | default(vmagent_user) }}

View File

@ -0,0 +1,9 @@
---
- name: Ensure vmagent container '{{ vmagent_container_name }}' is restarted
community.docker.docker_container:
name: "{{ vmagent_container_name }}"
state: "{{ vmagent_container_state }}"
restart: true
listen: "vmagent-reload"
ignore_errors: "{{ ansible_check_mode }}"
when: vmagent_deployment_method == 'docker'

View File

@ -0,0 +1,10 @@
---
allow_duplicates: true
dependencies: []
galaxy_info:
role_name: vmagent
description: Deploy and configure the victoriametrics agent `vmagent`
galaxy_tags:
- victoriametrics
- vmagent
- prometheus

View File

@ -0,0 +1,27 @@
---
- name: Ensure container image '{{ vmagent_container_image }}' is {{ vmagent_state }}
community.docker.docker_image:
name: "{{ vmagent_container_image }}"
state: "{{ vmagent_state }}"
source: "{{ vmagent_container_image_source }}"
force_source: "{{ vmagent_container_image_force_source }}"
register: vmagent_container_image_info
until: vmagent_container_image_info is success
retries: "{{ vmagent_container_image_network_retries }}"
delay: "{{ vmagent_container_image_network_delay }}"
- name: Ensure container '{{ vmagent_container_name }}' is {{ vmagent_container_state }}
community.docker.docker_container:
name: "{{ vmagent_container_name }}"
image: "{{ vmagent_container_image }}"
env: "{{ vmagent_container_merged_env }}"
user: "{{ vmagent_container_user }}"
ports: "{{ vmagent_container_ports | default(omit, true) }}"
labels: "{{ vmagent_container_labels | default(omit, true) }}"
command: "{{ vmagent_container_command }}"
volumes: "{{ vmagent_container_all_volumes }}"
networks: "{{ vmagent_container_networks | default(omit, true) }}"
etc_hosts: "{{ vmagent_container_etc_hosts | default(omit, true) }}"
dns_servers: "{{ vmagent_container_dns_servers | default(omit, true) }}"
restart_policy: "{{ vmagent_container_restart_policy | default(omit, true) }}"
state: "{{ vmagent_container_state }}"

View File

@ -0,0 +1,54 @@
---
- name: Check that `vmagent_state` is valid
ansible.builtin.fail:
msg: >-2
Unsupported state '{{ vmagent_state }}'! Supported states
are {{ vmagent_states | join(', ') }}.
when: vmagent_state not in vmagent_states
- name: Check that `vmagent_deployment_method` is valid
ansible.builtin.fail:
msg: >-2
Unsupported deployment method '{{ vmagent_deployment_method }}'!
Supported are: {{ vmagent_deployment_methods | join(', ') }}.
when: vmagent_deployment_method not in vmagent_deployment_methods
- name: Ensure vmagent user '{{ vmagent_user }}' is {{ vmagent_state }}
ansible.builtin.user:
name: "{{ vmagent_user }}"
state: "{{ vmagent_state }}"
system: "{{ vmagent_user_system | default(true, true) }}"
groups: "{{ vmagent_user_groups | default(omit, true) }}"
append: "{{ (vmagent_user_groups | default([], true)) | length > 0 }}"
create_home: "{{ vmagent_user_create_home | default(false, true) }}"
register: vmagent_user_info
- name: Ensure configuration file '{{ vmagent_scrape_config_file }}' is {{ vmagent_state }}
ansible.builtin.file:
path: "{{ vmagent_scrape_config_file }}"
state: "{{ vmagent_state }}"
when: vmagent_state == 'absent'
- name: Ensure config directory '{{ vmagent_config_path }}' is {{ vmagent_state }}
ansible.builtin.file:
path: "{{ vmagent_config_path }}"
state: >-2
{{ (vmagent_state == 'present') | ternary('directory', 'absent') }}
owner: "{{ vmagent_run_user_id }}"
group: "{{ vmagent_run_group_id }}"
mode: "0755"
- name: Ensure configuration file '{{ vmagent_scrape_config_file }}' is {{ vmagent_state }}
ansible.builtin.copy:
dest: "{{ vmagent_scrape_config_file }}"
content: "{{ vmagent_merged_config | to_nice_yaml(indent=4, width=1000) }}"
owner: "{{ vmagent_run_user_id }}"
group: "{{ vmagent_run_group_id }}"
mode: "0644"
when: vmagent_state == 'present'
notify:
- vmagent-reload
- name: Ensure vmagent is deployed using {{ vmagent_deployment_method }}
ansible.builtin.include_tasks:
file: "deploy-{{ vmagent_deployment_method }}.yml"

View File

@ -0,0 +1,6 @@
---
vmagent_states:
- present
- absent
vmagent_deployment_methods:
- docker