Home I've created a Grafana datasource provisioning thingy
Post
Cancel

I've created a Grafana datasource provisioning thingy

I’ve created a Grafana datasource provisioning thingy

You wonder why I would do that and write about it?

Well, imagine the following scenario: you have a running Grafana setup, lot’s of dashboards and datasources. Have you ever tried to figure out which dashboard is using which datasource after all your datasources are gone for some reason? I did and it’s not fun.

Because re-creating all datasources will create different UID’s than the ones you had before. And if you have a lot of dashboards, you need to update all your dashboards with the new UID’s. And this is, again, not fun.

The Problem

Currently, when you create a dashboard in Grafana itself with existing datasources and you want to export your dashboard into a JSON file, the datasource name will not be exported. What will be exported is the datasource UID (unique identifier). That UID could look like this:

1
"uid": "3G89vcP5f"

So imagine you have a Grafana instance running on a node somewhere in the cloud or in your homelab. And for some reason Grafana goes belly up and you have to reinstall it.

You have a backup of your Grafana configuration and you want to restore it. You need to re-create all datasources including all dashboards but as you can imagine, this is a lot of work if you do this manually.

And maybe you have your dashboards already in code, but you still need to re-create all datasources. And you will see that if you re-create all datasources, the UID’s will be different. So you need to update all your dashboards with the new UID’s. And this is a lot of work.

The Solution

I’ve created a small python script which will do the following:

  1. It reads a JSON file which contains all datasources and their UID’s and all other important settings for that specific datasource type.
  2. It will then connect to your Grafana instance and create all datasources one by one. Why not all at once? Because if you have a lot of datasources, you will hit the API rate limit. So I’ve added a sleep timer to the script. You can change the sleep timer in the script if you want to.
  3. You need to have an API key or create one for your Grafana instance. The script will use this API key to create all datasources. Please note: the API key needs to have the following permissions: Admin: Read & Write
  4. The Grafana version I’m using is Grafana v10.0.1 (5a30620b85). I don’t know if this script will work with older versions of Grafana.

How to use it?

I’ve created a GitHub repository for this script. You can find it here: https://github.com/GPegel/grafana-datasource-creation

Final words

Please read the README.md carefully before you use this script. I’ve tested it on my Mac and it works fine. But I can’t guarantee that it will work on your machine. If you have any questions, please let me know.

This post is licensed under CC BY 4.0 by the author.