DEV Community

Nithya15aa
Nithya15aa

Posted on

Automating Dashboard Import for Grafana Standalone Setup using Provisioning

As a DevOps engineer, I completely understand how manually importing dashboards can be a time-consuming task. Luckily, Grafana provides several ways to automate the process of importing dashboards.manual import

One way to automate the import process is through the use of Grafana’s API. Grafana’s API allows you to programmatically manage your Grafana instance, including importing dashboards. The API supports importing dashboards in JSON format, which means you can automate the entire process of importing dashboards using a script or another automation tool.While iterating through an entire folder to import dashboards using Ansible tasks can be a useful approach for automating dashboard imports, it can also require a lot of attention to detail and calculation to ensure the dashboard is imported correctly and that the directory structure is preserved in Grafana.What if it were as simple as placing the dashboard JSON files in the provisioning folder and letting Grafana handle the rest?

What is Grafana Provisioning?
Grafana Provisioning is the process of automating the setup and configuration of Grafana dashboards, data sources, and other settings. By using provisioning, you can easily deploy changes to multiple instances of Grafana, without having to manually make the same changes over and over again. Grafana provisioning is especially useful in environments where you have many instances of Grafana that need to be set up and maintained.

Getting Started with Grafana Provisioning

Step 1: Prepare Your Configuration

The first step in Grafana provisioning is to prepare your configuration files. Grafana provisioning uses YAML files to define data sources, dashboards, and other settings. You can create these files manually.

Here is the sample.yaml file

`apiVersion: 1

providers:
  # <string> an unique provider name. Required
  - name: 'a unique provider name'
    # <int> Org id. Default to 1
    orgId: 1
    # <string> name of the dashboard folder.
    folder: ''
    # <string> folder UID. will be automatically generated if not specified
    folderUid: ''
    # <string> provider type. Default to 'file'
    type: file
    # <bool> disable dashboard deletion
    disableDeletion: false
    # <int> how often Grafana will scan for changed dashboards
    updateIntervalSeconds: 10
    # <bool> allow updating provisioned dashboards from the UI
    allowUiUpdates: false
    options:
      # <string, required> path to dashboard files on disk. Required when using the 'file' type
      path: "{{ dest_dashboard_path }}"
      # <bool> use folder names from filesystem to create folders in Grafana
      foldersFromFilesStructure: true`
Enter fullscreen mode Exit fullscreen mode

Replace the dest_dashboard_path in the yaml with the path where you have stored your dashboard JSON files. You can choose the path according to your preference and file structure.

Step 2: Place your dashboards to dashboard path

Default dest_dashboard_path is /var/lib/grafana/dashboards/
Place your dashboards json files or directories in dest_dashboard_path.

Step 3: Configure Grafana

Edit your Grafana configuration file (grafana.ini) to specify the location of your configuration directory. You can do this by adding the following lines to your configuration file

[provisioning]

#Enable provisioning

enabled = true

#Specify the location of the provisioning files

provisioning_path = /etc/grafana/provisioning
Enter fullscreen mode Exit fullscreen mode

**
Step 4: Restart Grafana**

Finally, you’ll need to restart Grafana to load your configuration files. You can do this by running the following command:

sudo systemctl restart grafana-server

Once Grafana has restarted, it will automatically load your configuration files and apply any changes you’ve made.

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Ansible tasks to automate grafana dashboard provisioning

add below tasks to roles/grafana/tasks/main.yaml file

- name: " Copy Dashboards to grafana server"
  copy: 
    src: "{{ src_dashboard_path }}"
    dest: "{{ dest_dashboard_path }}"
    owner: grafana
    group: grafana
    mode: 0644

- name: " Copy dashboard provisioning config file"
  template:
    src: "sample.yaml"
    dest: "{{ provisioning_path }}"

- name: "Grafana configuration for provisioning"
  become: yes 
  ansible.builtin.lineinfile:
    path: "{{ gr_confFile }}/grafana.ini"
    line: |
      [provisioning]
      #Enable provisioning
      enabled = true
      #Specify the location of the provisioning files
      provisioning_path = /etc/grafana/provisioning
  notify: event_restart_grafana   

- name: "Grafana server started"
  service:
    name: grafana-server
    enabled: true
    state: started
Enter fullscreen mode Exit fullscreen mode

keep dashboards in ‘grafana/files/’ folder

your ‘group_vars/grafana’ variable file:

configFilePath: /etc/ #change according to your requirement
gr_confFile: "{{ configFilePath }}/grafana"
src_dashboard_path: "roles/grafana/files/"
dest_dashboard_path: /var/lib/grafana/dashboards/
provisioning_path: "{{ gr_confFile }}/provisioning/dashboards/"
Enter fullscreen mode Exit fullscreen mode

Playbook can look like this: grafana.yaml

- hosts: grafana
  become: yes
  become_user: root
  become_method: sudo
  roles:
    - grafana
Enter fullscreen mode Exit fullscreen mode

host file: hosts

[grafana]
xx.xx.xx.xx
#replace xx.xx.xx.xx with your host IP
Enter fullscreen mode Exit fullscreen mode

and run ‘

ansible-playbook -i hosts grafana.yaml
Enter fullscreen mode Exit fullscreen mode

Once Grafana has restarted, it will automatically load your configuration files and apply any changes you’ve made. Congratulations, you’re now using Grafana provisioning!

Top comments (0)