ansible-grafana icon indicating copy to clipboard operation
ansible-grafana copied to clipboard

Dashboard Organization

Open PhilThurston opened this issue 3 years ago • 5 comments

What is missing?

Right now any dashboards that are provisioned via Ansible are locked into being in the general folder along with the default org. Having the ability to list different provisioned dashboard providers can let us organize our systems.

Why do we need it?

As it's currently made the dashboards are cluttered and negates some of the built in Grafana features to let you organize and clean things up since provisioned dashboards can't be moved.

Environment

  • Role version:

    0.17.0

Anything else we need to know?:

I am willing to work on this if there is actual interest in having this feature.

PhilThurston avatar Aug 27 '20 22:08 PhilThurston

That would be very useful!

If you start working on it, please reuse as much of https://github.com/ansible-collections/community.grafana as possible instead of doing hacky things with uri and get_url modules as we, unfortunately, did in the past.

paulfantom avatar Sep 03 '20 08:09 paulfantom

I did it

image

Here dashboard.yml

---

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

# Create topology folders and copy dashboards

    - name: Create folders for dashboards from "files/office_dashboards"
      file:
         path: "{{ grafana_data_dir }}/dashboards/{{ item.path }}"
         state: directory
      with_filetree: files/office_dashboards
      when: item.state == 'directory'

    - name: Copy files
      copy:
        src: "{{ item.src }}"
        dest: "{{ grafana_data_dir }}/dashboards/{{ item.path }}"
      with_filetree: files/office_dashboards/
      when: item.state == 'file'

image

SergeiCherevko avatar Oct 28 '20 11:10 SergeiCherevko

Hi guys,

@inkdude what about providing a MR for this feature ?

Xat59 avatar Apr 27 '21 09:04 Xat59

I see this has not seen any update. This is what the full file looks like for anyone who wants to do this

---
- become: false
  delegate_to: localhost
  run_once: true
  block:
    - name: Create local grafana dashboard directory
      tempfile:
        state: directory
      register: _tmp_dashboards
      changed_when: false
      check_mode: false

    - name: Download grafana dashboard from grafana.com to local directory
      get_url:
        url: "https://grafana.com/api/dashboards/{{ item.dashboard_id }}/revisions/{{ item.revision_id }}/download"
        dest: "{{ _tmp_dashboards.path }}/{{ item.dashboard_id }}.json"
      register: _download_dashboards
      until: _download_dashboards is succeeded
      retries: 5
      delay: 2
      with_items: "{{ grafana_dashboards }}"
      when: grafana_dashboards | length > 0
      changed_when: false
      check_mode: false
      tags:
        - skip_ansible_lint

    - name: Set the correct data source name in the dashboard
      replace:
        dest: "{{ _tmp_dashboards.path }}/{{ item.dashboard_id }}.json"
        regexp: '"(?:\${)?DS_[A-Z0-9_-]+(?:})?"'
        replace: '"{{ item.datasource }}"'
      changed_when: false
      with_items: "{{ grafana_dashboards }}"
      when: grafana_dashboards | length > 0

- name: Import grafana dashboards through API
  uri:
    url: "{{ grafana_api_url }}/api/dashboards/db"
    user: "{{ grafana_security.admin_user }}"
    password: "{{ grafana_security.admin_password }}"
    force_basic_auth: true
    method: POST
    body_format: json
    body: >
      {
        "dashboard": {{ lookup("file", item) }},
        "overwrite": true,
        "message": "Updated by ansible"
      }
  no_log: true
  with_fileglob:
    - "{{ _tmp_dashboards.path }}/*"
    - "{{ grafana_dashboards_dir }}/*.json"
  when: not grafana_use_provisioning

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

# Create topology folders and copy dashboards

    - name: Create folders for dashboards from "_dashboards"
      file:
         path: "{{ grafana_data_dir }}/dashboards/{{ item.path }}"
         state: directory
      with_community.general.filetree: dashboards
      when: item.state == 'directory'

    - name: Copy files
      copy:
        src: "{{ item.src }}"
        dest: "{{ grafana_data_dir }}/dashboards/{{ item.path }}"
      with_community.general.filetree: dashboards/
      when: item.state == 'file'

    - name: Register previously copied dashboards
      find:
        paths: "{{ grafana_data_dir }}/dashboards"
        hidden: true
        patterns:
          - "*.json"
          - "*/*.json"
      register: _dashboards_present
      when: grafana_provisioning_synced

    - name: Import grafana dashboards
      become: true
      copy:
        src: "{{ item }}"
        dest: "{{ grafana_data_dir }}/dashboards/{{ item | basename }}"
      with_fileglob:
        - "{{ _tmp_dashboards.path }}/*"
        - "{{ grafana_dashboards_dir }}/*.json"
      register: _dashboards_copied
      notify: "provisioned dashboards changed"

    - name: Get dashboard lists
      set_fact:
        _dashboards_present_list: "{{ _dashboards_present | json_query('files[*].path') | default([]) }}"
        _dashboards_copied_list: "{{ _dashboards_copied | json_query('results[*].dest') | default([]) }}"
      when: grafana_provisioning_synced

    - name: Remove dashboards not present on deployer machine (synchronize)
      become: true
      file:
        path: "{{ item }}"
        state: absent
      with_items: "{{ _dashboards_present_list | difference( _dashboards_copied_list ) }}"
      when: grafana_provisioning_synced

SiM22 avatar Jul 16 '22 00:07 SiM22

Created PR - https://github.com/cloudalchemy/ansible-grafana/pull/294

SiM22 avatar Jul 16 '22 00:07 SiM22

Hey @SiM22,

How you use that in playbook? I use your fork as role.

omertahaoztop avatar Sep 27 '22 10:09 omertahaoztop

Hey @SiM22,

How you use that in playbook? I use your fork as role.

It is part of the role. All you need to do is add your yaml files for your dashboards to a directory called "dashboards" in the root of the role

SiM22 avatar Sep 27 '22 12:09 SiM22

This role has been deprecated in favor of a the grafana-ansible-collection collection.

SuperQ avatar May 31 '23 04:05 SuperQ