diff --git a/ansible/roles/grafana/tasks/main.yml b/ansible/roles/grafana/tasks/main.yml index 4502c19..0134a62 100644 --- a/ansible/roles/grafana/tasks/main.yml +++ b/ansible/roles/grafana/tasks/main.yml @@ -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