From 52056c158b50363aead34df31833ecd931fd2dea Mon Sep 17 00:00:00 2001 From: dpavle Date: Mon, 15 Apr 2024 12:57:22 +0200 Subject: [PATCH 1/6] enhancement(prometheus): support prometheus2 .yml rule file format Signed-off-by: dpavle --- roles/prometheus/meta/argument_specs.yml | 4 ++-- roles/prometheus/templates/prometheus.yml.j2 | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/roles/prometheus/meta/argument_specs.yml b/roles/prometheus/meta/argument_specs.yml index 96555b49e..1ac90a303 100644 --- a/roles/prometheus/meta/argument_specs.yml +++ b/roles/prometheus/meta/argument_specs.yml @@ -130,14 +130,14 @@ argument_specs: prometheus_alert_rules: description: - "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.rules)." - - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have C(*.rules) extension" + - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have a C(*.rules) or C(*.rules.yml) extension" - "Please see default values in role defaults/main.yml" type: "list" elements: "dict" prometheus_alert_rules_files: description: - "List of folders where ansible will look for files containing alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/)." - - "Files must have C(*.rules) extension" + - "Files must have a C(*.rules) or C(*.rules.yml) extension" type: "list" elements: "str" default: diff --git a/roles/prometheus/templates/prometheus.yml.j2 b/roles/prometheus/templates/prometheus.yml.j2 index fb0be5d2f..201ab35ca 100644 --- a/roles/prometheus/templates/prometheus.yml.j2 +++ b/roles/prometheus/templates/prometheus.yml.j2 @@ -20,6 +20,7 @@ remote_read: {% if not prometheus_agent_mode and prometheus_alert_rules_files != [] %} rule_files: - {{ prometheus_config_dir }}/rules/*.rules + - {{ prometheus_config_dir }}/rules/*.rules.yml {% endif %} {% if prometheus_alertmanager_config | length > 0 %} From 3eff65f47d873c6af98c7dccc029b812c58c16da Mon Sep 17 00:00:00 2001 From: Pavle <17710777+dpavle@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:45:48 +0200 Subject: [PATCH 2/6] enhancement(prometheus): match any .yml file in /rules Co-authored-by: Ben Kochie Signed-off-by: Pavle <17710777+dpavle@users.noreply.github.com> --- roles/prometheus/templates/prometheus.yml.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roles/prometheus/templates/prometheus.yml.j2 b/roles/prometheus/templates/prometheus.yml.j2 index 201ab35ca..1cc8a95e7 100644 --- a/roles/prometheus/templates/prometheus.yml.j2 +++ b/roles/prometheus/templates/prometheus.yml.j2 @@ -20,7 +20,7 @@ remote_read: {% if not prometheus_agent_mode and prometheus_alert_rules_files != [] %} rule_files: - {{ prometheus_config_dir }}/rules/*.rules - - {{ prometheus_config_dir }}/rules/*.rules.yml + - {{ prometheus_config_dir }}/rules/*.yml {% endif %} {% if prometheus_alertmanager_config | length > 0 %} From e8b9766d7e1540cab1237d58c660eb85b04750e3 Mon Sep 17 00:00:00 2001 From: dpavle <17710777+dpavle@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:27:09 +0200 Subject: [PATCH 3/6] enhancement(prometheus): match any .yml file in /rules: docs Signed-off-by: dpavle <17710777+dpavle@users.noreply.github.com> --- roles/prometheus/meta/argument_specs.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/roles/prometheus/meta/argument_specs.yml b/roles/prometheus/meta/argument_specs.yml index 1ac90a303..1607946ea 100644 --- a/roles/prometheus/meta/argument_specs.yml +++ b/roles/prometheus/meta/argument_specs.yml @@ -130,14 +130,14 @@ argument_specs: prometheus_alert_rules: description: - "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.rules)." - - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have a C(*.rules) or C(*.rules.yml) extension" + - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have a C(*.yml) extension" - "Please see default values in role defaults/main.yml" type: "list" elements: "dict" prometheus_alert_rules_files: description: - "List of folders where ansible will look for files containing alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/)." - - "Files must have a C(*.rules) or C(*.rules.yml) extension" + - "Files must have a C(*.yml) extension" type: "list" elements: "str" default: From b8e4f317795c3a32989e2c2693c940fdc72903c1 Mon Sep 17 00:00:00 2001 From: dpavle <17710777+dpavle@users.noreply.github.com> Date: Wed, 17 Apr 2024 12:23:13 +0200 Subject: [PATCH 4/6] enhancement(prometheus): fully commit to the yaml rule format Signed-off-by: dpavle <17710777+dpavle@users.noreply.github.com> --- roles/prometheus/defaults/main.yml | 3 ++- roles/prometheus/meta/argument_specs.yml | 9 +++++---- .../molecule/alternative/tests/test_alternative.py | 2 +- roles/prometheus/molecule/default/tests/test_default.py | 2 +- roles/prometheus/tasks/configure.yml | 4 ++-- .../templates/{alert.rules.j2 => ansible_managed.yml.j2} | 0 roles/prometheus/templates/prometheus.yml.j2 | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) rename roles/prometheus/templates/{alert.rules.j2 => ansible_managed.yml.j2} (100%) diff --git a/roles/prometheus/defaults/main.yml b/roles/prometheus/defaults/main.yml index 5d9416c98..f348ae18e 100644 --- a/roles/prometheus/defaults/main.yml +++ b/roles/prometheus/defaults/main.yml @@ -91,7 +91,8 @@ prometheus_scrape_configs: prometheus_config_file: 'prometheus.yml.j2' prometheus_alert_rules_files: - - prometheus/rules/*.rules + - prometheus/rules/*.yml + - prometheus/rules/*.yaml prometheus_static_targets_files: - prometheus/targets/*.yml diff --git a/roles/prometheus/meta/argument_specs.yml b/roles/prometheus/meta/argument_specs.yml index ed3a55201..9a014f8a6 100644 --- a/roles/prometheus/meta/argument_specs.yml +++ b/roles/prometheus/meta/argument_specs.yml @@ -129,19 +129,20 @@ argument_specs: default: "prometheus.yml.j2" prometheus_alert_rules: description: - - "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.rules)." - - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have a C(*.yml) extension" + - "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.yml)." + - "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have a C(*.yml) or C(*.yaml) extension" - "Please see default values in role defaults/main.yml" type: "list" elements: "dict" prometheus_alert_rules_files: description: - "List of folders where ansible will look for files containing alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/)." - - "Files must have a C(*.yml) extension" + - "Files must have a C(*.yml) or C(*.yaml) extension" type: "list" elements: "str" default: - - "prometheus/rules/*.rules" + - "prometheus/rules/*.yml" + - "prometheus/rules/*.yaml" prometheus_static_targets_files: description: - "List of folders where ansible will look for files containing custom static target configuration files which will be copied to C({{ prometheus_config_dir }}/file_sd/)." diff --git a/roles/prometheus/molecule/alternative/tests/test_alternative.py b/roles/prometheus/molecule/alternative/tests/test_alternative.py index 8635a866b..d6ce85e1a 100644 --- a/roles/prometheus/molecule/alternative/tests/test_alternative.py +++ b/roles/prometheus/molecule/alternative/tests/test_alternative.py @@ -23,7 +23,7 @@ def test_directories(host, dirs): @pytest.mark.parametrize("files", [ "/opt/prom/etc/prometheus.yml", - "/opt/prom/etc/rules/ansible_managed.rules", + "/opt/prom/etc/rules/ansible_managed.yml", "/opt/prom/etc/file_sd/node.yml", "/opt/prom/etc/file_sd/docker.yml", "/usr/local/bin/prometheus", diff --git a/roles/prometheus/molecule/default/tests/test_default.py b/roles/prometheus/molecule/default/tests/test_default.py index da6305b69..f180d03d1 100644 --- a/roles/prometheus/molecule/default/tests/test_default.py +++ b/roles/prometheus/molecule/default/tests/test_default.py @@ -46,7 +46,7 @@ def test_files(host, files): @pytest.mark.parametrize("files", [ - "/etc/prometheus/rules/ansible_managed.rules" + "/etc/prometheus/rules/ansible_managed.yml" ]) def test_absent(host, files): f = host.file(files) diff --git a/roles/prometheus/tasks/configure.yml b/roles/prometheus/tasks/configure.yml index 48ef8d584..64c9cbd1b 100644 --- a/roles/prometheus/tasks/configure.yml +++ b/roles/prometheus/tasks/configure.yml @@ -1,8 +1,8 @@ --- - name: Alerting rules file ansible.builtin.template: - src: "alert.rules.j2" - dest: "{{ prometheus_config_dir }}/rules/ansible_managed.rules" + src: "ansible_managed.yml.j2" + dest: "{{ prometheus_config_dir }}/rules/ansible_managed.yml" owner: root group: "{{ prometheus_system_group }}" mode: 0640 diff --git a/roles/prometheus/templates/alert.rules.j2 b/roles/prometheus/templates/ansible_managed.yml.j2 similarity index 100% rename from roles/prometheus/templates/alert.rules.j2 rename to roles/prometheus/templates/ansible_managed.yml.j2 diff --git a/roles/prometheus/templates/prometheus.yml.j2 b/roles/prometheus/templates/prometheus.yml.j2 index 1cc8a95e7..dbfb21d8b 100644 --- a/roles/prometheus/templates/prometheus.yml.j2 +++ b/roles/prometheus/templates/prometheus.yml.j2 @@ -19,8 +19,8 @@ remote_read: {% if not prometheus_agent_mode and prometheus_alert_rules_files != [] %} rule_files: - - {{ prometheus_config_dir }}/rules/*.rules - {{ prometheus_config_dir }}/rules/*.yml + - {{ prometheus_config_dir }}/rules/*.yaml {% endif %} {% if prometheus_alertmanager_config | length > 0 %} From a7b4409840ce2d8b3cc973172e82a06406e36c6d Mon Sep 17 00:00:00 2001 From: dpavle <17710777+dpavle@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:28:35 +0200 Subject: [PATCH 5/6] enhancement(prometheus): handle yaml formatted files with the old .rules extension Signed-off-by: dpavle <17710777+dpavle@users.noreply.github.com> --- roles/prometheus/tasks/preflight.yml | 12 ++++++++++++ roles/prometheus/templates/prometheus.yml.j2 | 1 + 2 files changed, 13 insertions(+) diff --git a/roles/prometheus/tasks/preflight.yml b/roles/prometheus/tasks/preflight.yml index 22ba42e6d..317fed9a6 100644 --- a/roles/prometheus/tasks/preflight.yml +++ b/roles/prometheus/tasks/preflight.yml @@ -89,6 +89,18 @@ - prometheus_alertmanager_config == [] - prometheus_alert_rules != [] +- name: Alert when alert rules files are found in the old .rules format + ansible.builtin.debug: + msg: > + prometheus_alert_rules_files contains rules in the old .rules file format. + This format has been deprecated in favour of the new YAML format since Prometheus v2.x. + You can update your rules using promtool: promtool update rules + + If your rules are already in the YAML format but with the .rules extension, you can safely ignore this message, + or rename the files to .yaml or .yml to get rid of it. + when: prometheus_alert_rules_files | select('search', '.rules$') | list | length > 0 + ignore_errors: true + - name: Discover latest version ansible.builtin.set_fact: prometheus_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _prometheus_repo}}/releases/latest', headers=_github_api_headers, diff --git a/roles/prometheus/templates/prometheus.yml.j2 b/roles/prometheus/templates/prometheus.yml.j2 index dbfb21d8b..aea786b10 100644 --- a/roles/prometheus/templates/prometheus.yml.j2 +++ b/roles/prometheus/templates/prometheus.yml.j2 @@ -21,6 +21,7 @@ remote_read: rule_files: - {{ prometheus_config_dir }}/rules/*.yml - {{ prometheus_config_dir }}/rules/*.yaml + - {{ prometheus_config_dir }}/rules/*.rules {% endif %} {% if prometheus_alertmanager_config | length > 0 %} From 0895a2c2a2ce084fbeaf91642cb35bcd00ffc0b0 Mon Sep 17 00:00:00 2001 From: dpavle <17710777+dpavle@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:47:05 +0200 Subject: [PATCH 6/6] enhancement(prometheus): fix linting errors Signed-off-by: dpavle <17710777+dpavle@users.noreply.github.com> --- roles/prometheus/tasks/preflight.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/roles/prometheus/tasks/preflight.yml b/roles/prometheus/tasks/preflight.yml index 317fed9a6..03704616d 100644 --- a/roles/prometheus/tasks/preflight.yml +++ b/roles/prometheus/tasks/preflight.yml @@ -99,7 +99,6 @@ If your rules are already in the YAML format but with the .rules extension, you can safely ignore this message, or rename the files to .yaml or .yml to get rid of it. when: prometheus_alert_rules_files | select('search', '.rules$') | list | length > 0 - ignore_errors: true - name: Discover latest version ansible.builtin.set_fact: