Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement(prometheus): support prometheus2 .yml rule file format #333

Merged
merged 8 commits into from
Oct 31, 2024
3 changes: 2 additions & 1 deletion roles/prometheus/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions roles/prometheus/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 C(*.rules) 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 C(*.rules) 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/)."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion roles/prometheus/molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions roles/prometheus/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 11 additions & 0 deletions roles/prometheus/tasks/preflight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@
- 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 <filenames>

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 <filename>.yaml or <filename>.yml to get rid of it.
when: prometheus_alert_rules_files | select('search', '.rules$') | list | length > 0

- 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,
Expand Down
2 changes: 2 additions & 0 deletions roles/prometheus/templates/prometheus.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ remote_read:

{% if not prometheus_agent_mode and prometheus_alert_rules_files != [] %}
rule_files:
- {{ prometheus_config_dir }}/rules/*.yml
- {{ prometheus_config_dir }}/rules/*.yaml
- {{ prometheus_config_dir }}/rules/*.rules
{% endif %}

Expand Down
Loading