Skip to content

Fix dashboard idempotency #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions ansible/roles/grafana-dashboards/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,40 @@
mode: 0640
notify: restart grafana

- name: Register previously copied dashboards
- name: Register preexisting dashboards
become: true
find:
paths: "{{ grafana_data_dir }}/dashboards"
hidden: true
patterns:
- "*.json"
register: _dashboards_present
register: _dashboards_pre

- name: Import grafana dashboards
become: true
copy:
remote_src: yes
src: "{{ _tmp_dashboards.path }}/" # Note trailing / to only copy contents, not directory itself
dest: "{{ grafana_data_dir }}/dashboards/"
register: _dashboards_copied
notify: "provisioned dashboards changed"

- name: Register all installed dashboards
become: true
find:
paths: "{{ grafana_data_dir }}/dashboards"
hidden: true
patterns:
- "*.json"
register: _dashboards_post

- name: Get dashboard lists
set_fact:
_dashboards_present_list: "{{ _dashboards_present | json_query('files[*].path') | default([]) }}"
_dashboards_copied_list: "{{ _dashboards_copied | json_query('results[*].dest') | default([]) }}"
_dashboards_pre_list: "{{ _dashboards_pre | json_query('files[*].path') | default([]) }}"
_dashboards_post_list: "{{ _dashboards_post | json_query('files[*].path') | default([]) }}"

- name: Remove installed dashboards not defined through this role
become: true
file:
path: "{{ item }}"
state: absent
with_items: "{{ _dashboards_present_list | difference( _dashboards_copied_list ) }}"
with_items: "{{ _dashboards_pre_list | difference( _dashboards_post_list ) }}"