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

Commit 27c9814

Browse files
mxbossardpaulfantom
authored andcommitted
Feature provisioning dashboards (#121)
* Fix grafana provisioning dir scaned on remote host rathen on localhost. And add missing tags. * Add parameter and tasks to use grafana dashboard provsioning like it already exists for datasources. * Fix yaml lint, and remove unused commented task. * Add a rsync install task when using grafana_provisioning. * Fix dashboard permissions for idempotency. * Move rsync installation to molecule default prepare playbook. Add a note about rsync need in README. * Fix molecule default prepare playbook by removing bad condition. * Move rsync requirement to requirements section. * Add "provisioned dashboards changed" handler. * Replace when "provisioned dashboards changed" tasks by a handler notification. * Fix lint: remove trailing spaces [minor] release
1 parent 0eb9db9 commit 27c9814

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ Provision and manage [grafana](https://github.com/grafana/grafana) - platform fo
1515
- Ansible >= 2.5
1616
- libselinux-python on deployer host (only when deployer machine has SELinux)
1717
- grafana >= 5.1 (for older grafana versions use this role in version 0.10.1 or earlier)
18+
- rsync if you plan to use grafana provisioning
1819

1920
## Role Variables
2021

2122
All variables which can be overridden are stored in [defaults/main.yml](defaults/main.yml) file as well as in table below.
2223

2324
| Name | Default Value | Description |
2425
| -------------- | ------------- | -----------------------------------|
25-
| `grafana_use_provisioning` | true | Use Grafana provisioning capalibity when possible (**grafana_version=latest will assume >= 5.0**) |
26+
| `grafana_use_provisioning` | true | Use Grafana provisioning capalibity when possible (**grafana_version=latest will assume >= 5.0**). |
27+
| `grafana_provisioning_synced` | false | Ensure no previously provisioned dashboards are kept if not referenced anymore. |
2628
| `grafana_system_user` | grafana | Grafana server system user |
2729
| `grafana_system_group` | grafana | Grafana server system group |
2830
| `grafana_version` | latest | Grafana package version |

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ grafana_yum_repo_template: etc/yum.repos.d/grafana.repo.j2
55
# Should we use the provisioning capability when possible (provisioning require grafana >= 5.0)
66
grafana_use_provisioning: true
77

8+
# Should the provisioning be kept synced. If true, previous provisioned objects will be removed if not referenced anymore.
9+
grafana_provisioning_synced: false
10+
811
grafana_instance: "{{ ansible_fqdn | default(ansible_host) | default(inventory_hostname) }}"
912

1013
grafana_logs_dir: "/var/log/grafana"

handlers/main.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,22 @@
44
service:
55
name: grafana-server
66
state: restarted
7+
8+
- name: Set privileges on provisioned dashboards
9+
become: true
10+
file:
11+
path: "/var/lib/grafana/dashboards"
12+
recurse: true
13+
owner: grafana
14+
group: grafana
15+
mode: 0640
16+
listen: "provisioned dashboards changed"
17+
18+
- name: Set privileges on provisioned dashboards directory
19+
become: true
20+
file:
21+
path: "/var/lib/grafana/dashboards"
22+
state: directory
23+
recurse: false
24+
mode: 0755
25+
listen: "provisioned dashboards changed"

molecule/default/prepare.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
- name: Prepare
33
hosts: all
44
gather_facts: false
5-
tasks: []
5+
tasks:
6+
- name: Install rsync for grafana dashboard provisioning
7+
package:
8+
name: ["rsync"]

tasks/dashboards.yml

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
---
2+
- name: Empty local grafana dashboard directory
3+
become: false
4+
file:
5+
path: /tmp/dashboards
6+
state: absent
7+
delegate_to: localhost
8+
run_once: true
9+
check_mode: false
10+
changed_when: false
11+
when: grafana_use_provisioning and grafana_provisioning_synced
12+
213
- name: Create local grafana dashboard directories
314
become: false
415
file:
@@ -8,6 +19,7 @@
819
delegate_to: localhost
920
run_once: true
1021
check_mode: false
22+
changed_when: false
1123

1224
# - name: download grafana dashboard from grafana.net to local folder
1325
# become: false
@@ -96,8 +108,9 @@
96108
- "{{ grafana_dashboards_dir }}/*.json"
97109
delegate_to: localhost
98110
run_once: true
111+
changed_when: false
99112

100-
- name: import grafana dashboards
113+
- name: import grafana dashboards through API
101114
uri:
102115
url: "{{ grafana_api_url }}/api/dashboards/db"
103116
user: "{{ grafana_security.admin_user }}"
@@ -109,6 +122,7 @@
109122
no_log: true
110123
with_fileglob:
111124
- "/tmp/dashboards/*"
125+
when: not grafana_use_provisioning
112126

113127
# TODO: uncomment this when ansible 2.7 will be min supported version
114128
# - name: import grafana dashboards
@@ -123,3 +137,36 @@
123137
# no_log: true
124138
# with_fileglob:
125139
# - "/tmp/dashboards/*"
140+
141+
- name: Create/Update dashboards file (provisioning)
142+
become: true
143+
copy:
144+
dest: "/etc/grafana/provisioning/dashboards/ansible.yml"
145+
content: |
146+
apiVersion: 1
147+
providers:
148+
- name: 'default'
149+
orgId: 1
150+
folder: ''
151+
type: file
152+
options:
153+
path: /var/lib/grafana/dashboards
154+
backup: false
155+
owner: root
156+
group: grafana
157+
mode: 0640
158+
notify: restart grafana
159+
when: grafana_use_provisioning
160+
161+
- name: Import grafana dashboards through provisioning
162+
synchronize:
163+
src: "/tmp/dashboards/"
164+
dest: "/var/lib/grafana/dashboards"
165+
archive: false
166+
checksum: true
167+
recursive: true
168+
delete: "{{ grafana_provisioning_synced }}"
169+
rsync_opts:
170+
- "--no-motd"
171+
when: grafana_use_provisioning
172+
notify: "provisioned dashboards changed"

tasks/main.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,12 @@
7979
find:
8080
paths: "{{ grafana_dashboards_dir }}"
8181
patterns: '*.json'
82-
register: found_dashboards
8382
delegate_to: localhost
8483
run_once: true
84+
register: found_dashboards
85+
tags:
86+
- grafana_configure
87+
- grafana_dashboards
8588

8689
- include: dashboards.yml
8790
when: grafana_dashboards | length > 0 or found_dashboards.matched > 0

0 commit comments

Comments
 (0)