Skip to content
This repository was archived by the owner on May 31, 2023. It is now read-only.

Commit 767649a

Browse files
authored
allow importing local dashboards (#80)
add option to import local dashboards
1 parent a0737ea commit 767649a

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults
5050
| `grafana_snapshots` | {} | [snapshots](http://docs.grafana.org/installation/configuration/#snapshots) configuration section |
5151
| `grafana_image_storage` | {} | [image storage](http://docs.grafana.org/installation/configuration/#external-image-storage) configuration section |
5252
| `grafana_dashboards` | [] | List of dashboards which should be imported |
53+
| `grafana_dashboards_dir` | "dashboards" | Path to a local directory containing dashboards files in `json` format |
5354
| `grafana_datasources` | [] | List of datasources which should be configured |
5455

5556
Datasource example:

defaults/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ grafana_dashboards: []
183183
# revision_id: '1'
184184
# datasource: 'Prometheus'
185185

186+
grafana_dashboards_dir: "dashboards"
187+
186188
# Alert notification channels to configure
187189
grafana_alert_notifications: []
188190
# - name: "Email Alert"

files/dashboards/.placeholder

Whitespace-only changes.

tasks/dashboards.yml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
delegate_to: localhost
99
run_once: true
1010

11-
- name: download grafana dashboard to local folder
11+
- name: download grafana dashboard from grafana.net to local folder
1212
become: false
1313
get_url:
1414
url: "https://grafana.com/api/dashboards/{{ item.dashboard_id }}/revisions/{{ item.revision_id }}/download"
@@ -20,6 +20,7 @@
2020
delegate_to: localhost
2121
run_once: true
2222
with_items: "{{ grafana_dashboards }}"
23+
when: grafana_dashboards | length > 0
2324

2425
# As noted in [1] an exported dashboard replaces the exporter's datasource
2526
# name with a representative name, something like 'DS_GRAPHITE'. The name
@@ -62,6 +63,17 @@
6263
delegate_to: localhost
6364
run_once: true
6465
with_items: "{{ grafana_dashboards }}"
66+
when: grafana_dashboards | length > 0
67+
68+
- name: copy local grafana dashboards
69+
become: false
70+
copy:
71+
src: "{{ item }}"
72+
dest: "/tmp/dashboards/{{ item | basename }}"
73+
with_fileglob:
74+
- "{{ grafana_dashboards_dir }}/*.json"
75+
delegate_to: localhost
76+
run_once: true
6577

6678
- name: import grafana dashboards
6779
uri:
@@ -71,7 +83,21 @@
7183
force_basic_auth: true
7284
method: POST
7385
body_format: json
74-
body: '{ "dashboard": {{ lookup("file", item) }}, "overwrite": true }'
86+
body: '{ "dashboard": {{ lookup("file", item) }}, "overwrite": true, "message": "Updated by ansible" }'
7587
no_log: true
7688
with_fileglob:
77-
- /tmp/dashboards/*
89+
- "/tmp/dashboards/*"
90+
91+
# TODO: uncomment this when ansible 2.7 will be min supported version
92+
# - name: import grafana dashboards
93+
# grafana_dashboard:
94+
# grafana_url: "{{ grafana_api_url }}"
95+
# grafana_user: "{{ grafana_security.admin_user }}"
96+
# grafana_password: "{{ grafana_security.admin_password }}"
97+
# path: "/tmp/dashboards/{{ item }}"
98+
# message: Updated by ansible
99+
# state: present
100+
# overwrite: true
101+
# no_log: true
102+
# with_fileglob:
103+
# - "/tmp/dashboards/*"

tasks/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
- notifications
5858

5959
- include: dashboards.yml
60-
when: grafana_dashboards | length > 0
6160
tags:
6261
- configure
6362
- dashboards

0 commit comments

Comments
 (0)