From 20b56e57ec0cf41cc19343ceb6e68ffd97382371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Manuel=20Dom=C3=ADnguez?= Date: Tue, 2 Jul 2024 17:31:56 +0200 Subject: [PATCH] Copy dashboards to the server before importing them when `grafana_use_provisioning` is `false` The module `community.grafana.grafana_dashboard` (used in roles/grafana/tasks/dashboards.yml) requires that the dashboards are present on the server. Copy the dashboards to a temporary directory on the server before attempting to import them. --- roles/grafana/tasks/dashboards.yml | 46 ++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/roles/grafana/tasks/dashboards.yml b/roles/grafana/tasks/dashboards.yml index 3fedb1f4..e1f43937 100644 --- a/roles/grafana/tasks/dashboards.yml +++ b/roles/grafana/tasks/dashboards.yml @@ -67,18 +67,40 @@ loop: "{{ grafana_dashboards }}" - name: "Import grafana dashboards via api" - community.grafana.grafana_dashboard: - grafana_url: "{{ grafana_api_url }}" - grafana_user: "{{ grafana_security.admin_user }}" - grafana_password: "{{ grafana_security.admin_password }}" - path: "{{ item }}" - message: "Updated by ansible role {{ ansible_role_name }}" - state: present - overwrite: true - no_log: "{{ 'false' if lookup('env', 'CI') else 'true' }}" - with_fileglob: - - "{{ __tmp_dashboards.path }}/*" - - "{{ grafana_dashboards_dir }}/*.json" + block: + - name: "Create temporary directory for dashboards" + ansible.builtin.tempfile: + prefix: grafana.grafana.grafana. + state: directory + changed_when: false + register: __tmp_dashboards_import + + - name: "Copy dashboards to the temporary directory" + ansible.builtin.copy: + src: "{{ grafana_dashboards_dir }}/" + dest: "{{ __tmp_dashboards_import.path }}/" + mode: "0440" + + - name: "Import dashboards via api" + community.grafana.grafana_dashboard: + grafana_url: "{{ grafana_api_url }}" + grafana_user: "{{ grafana_security.admin_user }}" + grafana_password: "{{ grafana_security.admin_password }}" + path: "{{ item }}" + commit_message: "Updated by ansible role {{ ansible_role_name }}" + state: present + overwrite: true + no_log: "{{ 'false' if lookup('env', 'CI') else 'true' }}" + with_fileglob: + - "{{ __tmp_dashboards.path }}/*" + - "{{ __tmp_dashboards_import.path }}/*.json" + always: + - name: "Clean up the temporary directory" + ansible.builtin.file: + path: "{{ __tmp_dashboards_import.path }}" + state: absent + when: __tmp_dashboards_import is defined + changed_when: false when: "not grafana_use_provisioning" - name: "Import grafana dashboards through provisioning"