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

Feature provisioning dashboards #121

Merged
Merged
Show file tree
Hide file tree
Changes from 9 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
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
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"]
66 changes: 65 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,53 @@
# 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:
paulfantom marked this conversation as resolved.
Show resolved Hide resolved
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
register: synchronize_result

- name: Set privileges on provisioned dashboards
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this task and next one are only executed when previous one was changed, then it might be better to use those as handlers instead of using when: synchronize_result.changed and registering variable. In the end that's what handlers are for. I don't know why this wasn't showing up in CI, but when I run it locally I immediately got linter errors:

    [ANSIBLE0016] Tasks that run when changed should likely be handlers
    /home/paulfantom/cloudalchemy/ansible-grafana/tasks/dashboards.yml:174
    Task/Handler: Set privileges on provisioned dashboards
    
    [ANSIBLE0016] Tasks that run when changed should likely be handlers
    /home/paulfantom/cloudalchemy/ansible-grafana/tasks/dashboards.yml:183
    Task/Handler: Set privileges on provisioned dashboards directory

Copy link
Contributor Author

@mxbossard mxbossard Jan 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes I see. I never used handlers for this kind of situation. I'll do that immediatly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can notify multiple handlers from one task like:

- name: doing sth
  command: echo "OK"
  notify:
    - do thing A
    - do thing B

file:
path: "/var/lib/grafana/dashboards"
recurse: true
owner: grafana
group: grafana
mode: 0640
when: grafana_use_provisioning and synchronize_result.changed

- name: Set privileges on provisioned dashboards directory
file:
path: "/var/lib/grafana/dashboards"
state: directory
recurse: false
mode: 0755
when: grafana_use_provisioning and synchronize_result.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