allow to remove non-referenced custom rule files

Signed-off-by: Sergio Charpinel <sergio.charpinel@ebury.com>
This commit is contained in:
Sergio Charpinel 2024-11-05 13:49:20 -03:00
parent 2a37662a46
commit e5d7fe3e78
3 changed files with 42 additions and 0 deletions

View file

@ -101,6 +101,8 @@ prometheus_scrape_config_files:
- prometheus/scrape_configs/*.yml - prometheus/scrape_configs/*.yml
- prometheus/scrape_configs/*.json - prometheus/scrape_configs/*.json
prometheus_provisioning_synced: false
# yamllint disable rule:line-length # yamllint disable rule:line-length
prometheus_alert_rules: # noqa yaml[line-length] # noqa line-length prometheus_alert_rules: # noqa yaml[line-length] # noqa line-length
- alert: Watchdog - alert: Watchdog

View file

@ -139,6 +139,11 @@ argument_specs:
default: default:
- "prometheus/rules/*.yml" - "prometheus/rules/*.yml"
- "prometheus/rules/*.yaml" - "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: prometheus_static_targets_files:
description: 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/)." - "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/)."

View file

@ -74,6 +74,7 @@
when: when:
- prometheus_alert_rules != [] - prometheus_alert_rules != []
- not prometheus_agent_mode - not prometheus_agent_mode
register: __rules_managed_copied
notify: notify:
- reload prometheus - reload prometheus
become: true become: true
@ -82,6 +83,16 @@
- configure - configure
- prometheus_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 - name: Copy custom alerting rule files
ansible.builtin.copy: ansible.builtin.copy:
src: "{{ item }}" src: "{{ item }}"
@ -96,6 +107,30 @@
notify: notify:
- reload prometheus - reload prometheus
become: true 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: tags:
- prometheus - prometheus
- configure - configure