From fc136a60a09c7b44ff2c628f9a0bc8d0aa834f84 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 29 Aug 2023 20:51:02 +0200 Subject: grafana: delete automatically installed dashboards that are no longer needed --- roles/monitoring/grafana/tasks/main.yml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'roles') diff --git a/roles/monitoring/grafana/tasks/main.yml b/roles/monitoring/grafana/tasks/main.yml index 30f1348e..48a0ec96 100644 --- a/roles/monitoring/grafana/tasks/main.yml +++ b/roles/monitoring/grafana/tasks/main.yml @@ -81,7 +81,7 @@ no_log: yes notify: restart grafana -- name: install datasources +- name: configure dashboard provisioning copy: dest: "/etc/grafana/provisioning/dashboards/ansible.yml" content: | @@ -110,6 +110,7 @@ copy: content: "{{ lookup('file', 'dashboard-'+item.file+'.json') | regex_replace('\"(?:\\${)?DS_[A-Z0-9_-]+(?:})?\"', '\"'+item.datasource+'\"') }}\n" dest: "/var/lib/grafana/dashboards/ansible/{{ item.datasource }}_{{ item.file }}.json" + register: grafana_dasboards_from_file - name: install dashboards from content loop: "{{ grafana_dashboards | selectattr('content', 'defined') }}" @@ -118,6 +119,7 @@ copy: content: "{{ item.content | regex_replace('\"(?:\\${)?DS_[A-Z0-9_-]+(?:})?\"', '\"'+item.datasource+'\"') }}\n" dest: "/var/lib/grafana/dashboards/ansible/{{ item.datasource }}_{{ item.name }}.json" + register: grafana_dasboards_from_content ## TODO: implement this - name: install dashboards from grafana.com @@ -126,6 +128,19 @@ label: "https://grafana.com/api/dashboards/{{ item.id }} -> {{ item.datasource }}" debug: msg: 'installing dashboards from grafana.com is not yet supported!' + # register: grafana_dasboards_from_grafanacom + +- name: fetch list of installed dashboards + find: + paths: /var/lib/grafana/dashboards/ansible + patterns: "*.json" + register: grafana_dashboards_installed + +- name: remove superfluous dashboards + loop: "{{ grafana_dashboards_installed.files | map(attribute='path') | difference(grafana_dasboards_from_file.results | map(attribute='dest')) | difference(grafana_dasboards_from_content.results | map(attribute='dest')) | list }}" + file: + path: "{{ item }}" + state: absent - name: make sure grafana-server is enabled and started systemd: -- cgit v1.2.3