diff --git a/README.md b/README.md index 8117ebb..805d218 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ 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 @@ -22,7 +23,8 @@ All variables which can be overridden are stored in [defaults/main.yml](defaults | 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 | diff --git a/defaults/main.yml b/defaults/main.yml index 2f497a3..6ecfadc 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -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" diff --git a/handlers/main.yml b/handlers/main.yml index 9777226..ba9c812 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -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" diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index 5358b3b..354904d 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -2,4 +2,7 @@ - name: Prepare hosts: all gather_facts: false - tasks: [] + tasks: + - name: Install rsync for grafana dashboard provisioning + package: + name: ["rsync"] diff --git a/tasks/dashboards.yml b/tasks/dashboards.yml index b152f77..33cd2ba 100644 --- a/tasks/dashboards.yml +++ b/tasks/dashboards.yml @@ -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: @@ -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 @@ -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 }}" @@ -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 @@ -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" diff --git a/tasks/main.yml b/tasks/main.yml index 75b0afe..373913e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -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