From e5d7fe3e78c61a07840fee4d5a9144a4605df1de Mon Sep 17 00:00:00 2001 From: Sergio Charpinel Date: Tue, 5 Nov 2024 13:49:20 -0300 Subject: [PATCH] allow to remove non-referenced custom rule files Signed-off-by: Sergio Charpinel --- roles/prometheus/defaults/main.yml | 2 ++ roles/prometheus/meta/argument_specs.yml | 5 ++++ roles/prometheus/tasks/configure.yml | 35 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/roles/prometheus/defaults/main.yml b/roles/prometheus/defaults/main.yml index 127e9696e..a835326c3 100644 --- a/roles/prometheus/defaults/main.yml +++ b/roles/prometheus/defaults/main.yml @@ -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 diff --git a/roles/prometheus/meta/argument_specs.yml b/roles/prometheus/meta/argument_specs.yml index b57fd708e..dcbfe909e 100644 --- a/roles/prometheus/meta/argument_specs.yml +++ b/roles/prometheus/meta/argument_specs.yml @@ -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/)." diff --git a/roles/prometheus/tasks/configure.yml b/roles/prometheus/tasks/configure.yml index dbf1b7b92..66e0154c6 100644 --- a/roles/prometheus/tasks/configure.yml +++ b/roles/prometheus/tasks/configure.yml @@ -74,6 +74,7 @@ when: - prometheus_alert_rules != [] - not prometheus_agent_mode + register: __rules_managed_copied notify: - reload prometheus become: true @@ -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 }}" @@ -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