Merge pull request #377 from manzsolutions-lpr/fix-scrape-config-path

fix: scrape config files path
This commit is contained in:
gardar 2024-10-25 16:46:18 +00:00 committed by GitHub
commit e2d8b745dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 104 additions and 40 deletions

View file

@ -97,8 +97,8 @@ prometheus_static_targets_files:
- prometheus/targets/*.json
prometheus_scrape_config_files:
- prometheus/targets/*.yml
- prometheus/targets/*.json
- prometheus/scrape_configs/*.yml
- prometheus/scrape_configs/*.json
# yamllint disable rule:line-length
prometheus_alert_rules: # noqa yaml[line-length] # noqa line-length

View file

@ -148,13 +148,13 @@ argument_specs:
- "prometheus/targets/*.json"
prometheus_scrape_config_files:
description:
- "List of folders where ansible will look for files containing custom scrape config configuration files which will be copied to C({{ prometheus_config_dir }}/scrapes/)."
- "List of folders where ansible will look for files containing custom scrape config configuration files which will be copied to C({{ prometheus_config_dir }}/scrape_configs/)."
- "This feature is available starting from Prometheus v2.43.0."
type: "list"
elements: "str"
default:
- "prometheus/scrapes/*.yml"
- "prometheus/scrapes/*.json"
- "prometheus/scrape_configs/*.yml"
- "prometheus/scrape_configs/*.json"
prometheus_system_group:
description:
- "System group for Prometheus."

View file

@ -1,7 +1,5 @@
---
provisioner:
playbooks:
prepare: "${MOLECULE_PROJECT_DIRECTORY}/../../.config/molecule/alternative/prepare.yml"
inventory:
group_vars:
all:
@ -58,6 +56,8 @@ provisioner:
- influx.example.org:8080
labels:
env: demo
prometheus_scrape_config_files:
- /tmp/prometheus/scrape_configs/*.yml
prometheus_scrape_configs:
- job_name: "prometheus"
metrics_path: "{{ prometheus_metrics_path }}"

View file

@ -0,0 +1,20 @@
---
- name: Run local preparation
hosts: localhost
gather_facts: false
tasks:
- name: Create scrape_configs directory
ansible.builtin.file:
name: /tmp/prometheus/scrape_configs
state: directory
recurse: true
check_mode: false
- name: Create empty scrape config file
ansible.builtin.file:
name: /tmp/prometheus/scrape_configs/empty_scrapes.yml
state: touch
mode: 0664
check_mode: false
- name: Import shared preparation playbook
import_playbook: "../../../../.config/molecule/alternative/prepare.yml"

View file

@ -7,26 +7,34 @@ import pytest
testinfra_hosts = get_target_hosts()
@pytest.mark.parametrize("dirs", [
@pytest.mark.parametrize(
"dirs",
[
"/opt/prom/etc",
"/opt/prom/etc/rules",
"/opt/prom/etc/file_sd",
"/opt/prom/lib"
])
"/opt/prom/etc/scrape_configs",
"/opt/prom/lib",
],
)
def test_directories(host, dirs):
d = host.file(dirs)
assert d.is_directory
assert d.exists
@pytest.mark.parametrize("files", [
@pytest.mark.parametrize(
"files",
[
"/opt/prom/etc/prometheus.yml",
"/opt/prom/etc/rules/ansible_managed.rules",
"/opt/prom/etc/file_sd/node.yml",
"/opt/prom/etc/file_sd/docker.yml",
"/opt/prom/etc/scrape_configs/empty_scrapes.yml",
"/usr/local/bin/prometheus",
"/usr/local/bin/promtool"
])
"/usr/local/bin/promtool",
],
)
def test_files(host, files):
f = host.file(files)
assert f.exists

View file

@ -0,0 +1,17 @@
---
- name: Run local preparation
hosts: localhost
gather_facts: false
tasks:
- name: Create scrape_configs directory
ansible.builtin.file:
name: "{{ playbook_dir }}/../../prometheus/scrape_configs"
state: directory
recurse: true
check_mode: false
- name: Create empty scrape config file
ansible.builtin.file:
name: "{{ playbook_dir }}/../../prometheus/scrape_configs/empty_scrapes.yml"
state: touch
mode: 0664
check_mode: false

View file

@ -15,28 +15,36 @@ def AnsibleDefaults():
return yaml.full_load(stream)
@pytest.mark.parametrize("dirs", [
@pytest.mark.parametrize(
"dirs",
[
"/etc/prometheus",
"/etc/prometheus/console_libraries",
"/etc/prometheus/consoles",
"/etc/prometheus/rules",
"/etc/prometheus/file_sd",
"/var/lib/prometheus"
])
"/etc/prometheus/scrape_configs",
"/var/lib/prometheus",
],
)
def test_directories(host, dirs):
d = host.file(dirs)
assert d.is_directory
assert d.exists
@pytest.mark.parametrize("files", [
@pytest.mark.parametrize(
"files",
[
"/etc/prometheus/prometheus.yml",
"/etc/prometheus/console_libraries/prom.lib",
"/etc/prometheus/consoles/prometheus.html",
"/etc/prometheus/scrape_configs/empty_scrapes.yml",
"/etc/systemd/system/prometheus.service",
"/usr/local/bin/prometheus",
"/usr/local/bin/promtool"
])
"/usr/local/bin/promtool",
],
)
def test_files(host, files):
f = host.file(files)
assert f.exists

View file

@ -38,7 +38,7 @@
loop:
- "{{ prometheus_config_dir }}/rules"
- "{{ prometheus_config_dir }}/file_sd"
- "{{ prometheus_config_dir }}/scrapes"
- "{{ prometheus_config_dir }}/scrape_configs"
become: true
tags:
- prometheus
@ -135,6 +135,8 @@
- prometheus
- configure
- prometheus_configure
notify:
- reload prometheus
- name: Copy prometheus custom static targets
ansible.builtin.copy:
@ -150,11 +152,13 @@
- prometheus
- configure
- prometheus_configure
notify:
- reload prometheus
- name: Copy prometheus scrape config files
ansible.builtin.copy:
src: "{{ item }}"
dest: "{{ prometheus_config_dir }}/scrapes/"
dest: "{{ prometheus_config_dir }}/scrape_configs/"
force: true
owner: "{{ prometheus_system_user }}"
group: "{{ prometheus_system_group }}"
@ -165,3 +169,10 @@
- prometheus
- configure
- prometheus_configure
notify:
- reload prometheus
- name: Remove "scrapes" folder replaced by "scrape_configs"
ansible.builtin.file:
path: "{{ prometheus_config_dir }}/scrapes"
state: absent

View file

@ -38,6 +38,6 @@ scrape_configs:
{% if prometheus_version is version('2.43.0', '>=') %}
{% if prometheus_scrape_config_files != [] %}
scrape_config_files:
- scrapes/*
- scrape_configs/*
{% endif %}
{% endif %}