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

feat: Add prometheus prometheus_provisioning_synced support #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions roles/prometheus/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ prometheus_scrape_config_files:
- prometheus/scrape_configs/*.yml
- prometheus/scrape_configs/*.json

prometheus_provisioning_synced: false

# yamllint disable rule:line-length
prometheus_alert_rules: # noqa yaml[line-length] # noqa line-length
- alert: Watchdog
Expand Down
5 changes: 5 additions & 0 deletions roles/prometheus/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ argument_specs:
default:
- "prometheus/rules/*.yml"
- "prometheus/rules/*.yaml"
prometheus_provisioning_synced:
description:
- "Should the provisioning of alert rule files be kept synced. If true, previous provisioned files will be removed if not referenced anymore."
type: bool
default: false
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
35 changes: 35 additions & 0 deletions roles/prometheus/tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
when:
- prometheus_alert_rules != []
- not prometheus_agent_mode
register: __rules_managed_copied
notify:
- reload prometheus
become: true
Expand All @@ -82,6 +83,16 @@
- configure
- prometheus_configure

- name: Register previously copied rules
ansible.builtin.find:
paths: "{{ prometheus_config_dir }}/rules/"
when: prometheus_provisioning_synced
register: __rules_present
tags:
- prometheus
- configure
- prometheus_configure

- name: Copy custom alerting rule files
ansible.builtin.copy:
src: "{{ item }}"
Expand All @@ -96,6 +107,30 @@
notify:
- reload prometheus
become: true
register: __rules_copied
tags:
- prometheus
- configure
- prometheus_configure

- name: Register present and copied rules
ansible.builtin.set_fact:
__rules_present_list: "{{ __rules_present | json_query('files[*].path') | default([]) }}"
__rules_copied_list: "{{ __rules_copied | json_query('results[*].dest') | default([]) }}"
__rules_managed_copied_list: "{{ [__rules_managed_copied | json_query('dest')] | default([]) }}"
when: prometheus_provisioning_synced
tags:
- prometheus
- configure
- prometheus_configure

- name: "Remove rules not present on deployer machine (synchronize)"
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop: "{{ __rules_present_list | difference(__rules_copied_list) | difference(__rules_managed_copied_list) }}"
become: true
when: prometheus_provisioning_synced and not ansible_check_mode
tags:
- prometheus
- configure
Expand Down
Loading