grafana: prune stale dashboard files from prom on each apply

Without this, a renamed dashboard left the old file on disk and the
provisioner refused to write any dashboards because of a duplicate UID
(seen with towerops-phoenix-live-view vs towerops-phoenix_live_view).
This commit is contained in:
Graham McIntire 2026-05-11 10:39:35 -05:00
parent 6fa9085b18
commit 5443e75d70
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -69,6 +69,34 @@
mode: "0644"
notify: Restart grafana
- name: List currently-provisioned dashboard files on host
ansible.builtin.find:
paths: "{{ grafana_dashboards_dir }}"
patterns: "*.json"
file_type: file
register: grafana_remote_dashboards
- name: List dashboard JSON files from this role
ansible.builtin.set_fact:
grafana_managed_dashboards: >-
{{
query('ansible.builtin.fileglob',
role_path ~ '/files/dashboards/*.json')
| map('basename') | list
}}
# Without this, a renamed dashboard leaves the old file on disk and the
# provisioner refuses to write any dashboards (duplicate UID warning).
- name: Remove stale dashboard files not present in the role
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ grafana_remote_dashboards.files }}"
loop_control:
label: "{{ item.path | basename }}"
when: (item.path | basename) not in grafana_managed_dashboards
notify: Restart grafana
- name: Render dashboard provider config
ansible.builtin.template:
src: dashboard-providers.yaml.j2