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

Commit

Permalink
Feature provisioning dashboards (#121)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mxbossard authored and paulfantom committed Jan 8, 2019
1 parent 0eb9db9 commit 27c9814
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ Provision and manage [grafana](https://github.com/grafana/grafana) - platform fo
- Ansible >= 2.5
- libselinux-python on deployer host (only when deployer machine has SELinux)
- grafana >= 5.1 (for older grafana versions use this role in version 0.10.1 or earlier)
- rsync if you plan to use grafana provisioning

## Role Variables

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

| Name | Default Value | Description |
| -------------- | ------------- | -----------------------------------|
| `grafana_use_provisioning` | true | Use Grafana provisioning capalibity when possible (**grafana_version=latest will assume >= 5.0**) |
| `grafana_use_provisioning` | true | Use Grafana provisioning capalibity when possible (**grafana_version=latest will assume >= 5.0**). |
| `grafana_provisioning_synced` | false | Ensure no previously provisioned dashboards are kept if not referenced anymore. |
| `grafana_system_user` | grafana | Grafana server system user |
| `grafana_system_group` | grafana | Grafana server system group |
| `grafana_version` | latest | Grafana package version |
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ grafana_yum_repo_template: etc/yum.repos.d/grafana.repo.j2
# Should we use the provisioning capability when possible (provisioning require grafana >= 5.0)
grafana_use_provisioning: true

# Should the provisioning be kept synced. If true, previous provisioned objects will be removed if not referenced anymore.
grafana_provisioning_synced: false

grafana_instance: "{{ ansible_fqdn | default(ansible_host) | default(inventory_hostname) }}"

grafana_logs_dir: "/var/log/grafana"
Expand Down
19 changes: 19 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@
service:
name: grafana-server
state: restarted

- name: Set privileges on provisioned dashboards
become: true
file:
path: "/var/lib/grafana/dashboards"
recurse: true
owner: grafana
group: grafana
mode: 0640
listen: "provisioned dashboards changed"

- name: Set privileges on provisioned dashboards directory
become: true
file:
path: "/var/lib/grafana/dashboards"
state: directory
recurse: false
mode: 0755
listen: "provisioned dashboards changed"
5 changes: 4 additions & 1 deletion molecule/default/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
- name: Prepare
hosts: all
gather_facts: false
tasks: []
tasks:
- name: Install rsync for grafana dashboard provisioning
package:
name: ["rsync"]
49 changes: 48 additions & 1 deletion tasks/dashboards.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
---
- name: Empty local grafana dashboard directory
become: false
file:
path: /tmp/dashboards
state: absent
delegate_to: localhost
run_once: true
check_mode: false
changed_when: false
when: grafana_use_provisioning and grafana_provisioning_synced

- name: Create local grafana dashboard directories
become: false
file:
Expand All @@ -8,6 +19,7 @@
delegate_to: localhost
run_once: true
check_mode: false
changed_when: false

# - name: download grafana dashboard from grafana.net to local folder
# become: false
Expand Down Expand Up @@ -96,8 +108,9 @@
- "{{ grafana_dashboards_dir }}/*.json"
delegate_to: localhost
run_once: true
changed_when: false

- name: import grafana dashboards
- name: import grafana dashboards through API
uri:
url: "{{ grafana_api_url }}/api/dashboards/db"
user: "{{ grafana_security.admin_user }}"
Expand All @@ -109,6 +122,7 @@
no_log: true
with_fileglob:
- "/tmp/dashboards/*"
when: not grafana_use_provisioning

# TODO: uncomment this when ansible 2.7 will be min supported version
# - name: import grafana dashboards
Expand All @@ -123,3 +137,36 @@
# no_log: true
# with_fileglob:
# - "/tmp/dashboards/*"

- name: Create/Update dashboards file (provisioning)
become: true
copy:
dest: "/etc/grafana/provisioning/dashboards/ansible.yml"
content: |
apiVersion: 1
providers:
- name: 'default'
orgId: 1
folder: ''
type: file
options:
path: /var/lib/grafana/dashboards
backup: false
owner: root
group: grafana
mode: 0640
notify: restart grafana
when: grafana_use_provisioning

- name: Import grafana dashboards through provisioning
synchronize:
src: "/tmp/dashboards/"
dest: "/var/lib/grafana/dashboards"
archive: false
checksum: true
recursive: true
delete: "{{ grafana_provisioning_synced }}"
rsync_opts:
- "--no-motd"
when: grafana_use_provisioning
notify: "provisioned dashboards changed"
5 changes: 4 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@
find:
paths: "{{ grafana_dashboards_dir }}"
patterns: '*.json'
register: found_dashboards
delegate_to: localhost
run_once: true
register: found_dashboards
tags:
- grafana_configure
- grafana_dashboards

- include: dashboards.yml
when: grafana_dashboards | length > 0 or found_dashboards.matched > 0
Expand Down

0 comments on commit 27c9814

Please sign in to comment.