Skip to content

Commit

Permalink
allow to remove non-referenced custom rule files
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Charpinel <[email protected]>
  • Loading branch information
sergiocharpineljr committed Nov 6, 2024
1 parent 2a37662 commit e5d7fe3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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

0 comments on commit e5d7fe3

Please sign in to comment.