A Python tool for building Grafana dashboards from Jinja2 templates and converting existing dashboards into reusable templates. This tool allows you to create dynamic, parameterized dashboards that can be easily maintained and deployed across different environments.
- Template-based Dashboard Generation: Create dashboards using Jinja2 templates with variables
- Multi-datasource Support: Generate dashboards for different datasources (InfluxDB, Prometheus, etc.)
- Multiple Output Formats: Export dashboards as JSON or YAML
- Dashboard Conversion: Convert existing Grafana dashboards into reusable templates
- Flexible Configuration: Configure outputs, datasources, and labels via YAML config
- Python 3.13+
- Dependencies listed in
pyproject.toml
- Clone the repository:
git clone <repository-url>
cd grafana-dashboards
- Install dependencies:
pip install -r requirements.txt
# or if using pip directly:
pip install jinja2>=3.1.6 pyyaml>=6.0.2
Generate dashboards from templates:
python3 builder.py build --config config.yml --templates templates --output output
Parameters:
--config
: Configuration file path (default:config.yml
)--templates
: Templates directory path (default:templates
)--output
: Output directory path (default:output
)
Convert existing Grafana dashboards to templates:
# Convert JSON dashboard
python3 builder.py convert --input dashboard.json --templates templates
# Convert YAML dashboard
python3 builder.py convert --input dashboard.yaml --templates templates
Parameters:
--input
: Path to dashboard JSON or YAML file (required)--templates
: Templates directory path (default:templates
)
output_format:
- json
- yaml
datasource:
- influxdb
- prometheus
labels:
- host
- name
- env
- namespace
- pod
target:
- dashboards
Configuration Options:
output_format
: List of output formats (json
,yaml
)datasource
: List of datasources to generate dashboards forlabels
: List of labels/variables to use in templatestarget
: List of target types to generate (dashboards
,alerts
, etc.)
Templates are organized in numbered directories:
templates/
├── 01_targets/ # Query targets/metrics
│ ├── cpu_usage.yml.j2
│ └── memory_usage.yml.j2
├── 01_variables/ # Dashboard variables
│ ├── hostname.yml.j2
│ └── environment.yml.j2
├── 01_inputs/ # Datasource inputs
│ └── prometheus.yml.j2
├── 02_panels/ # Individual panels
│ ├── cpu_panel.yml.j2
│ └── memory_panel.yml.j2
├── 03_rows/ # Row panels
│ └── system_metrics.yml.j2
└── 04_dashboards/ # Complete dashboards
└── system_overview.yml.j2
Templates use @{ }@
delimiters instead of {{ }}
to avoid conflicts with Grafana variables:
# Example panel template
title: "CPU Usage - @{ datasource }@"
targets:
- expr: 'cpu_usage{@{ prom_labels(labels) }@}'
datasource: "@{ datasource }@"
prom_labels(labels)
: Generate Prometheus label selectorsinflux_labels(labels)
: Generate InfluxDB WHERE clausesto_nice_yaml
: Convert objects to formatted YAML
datasource
: Current datasource being processedlabels
: List of available labelstargets
: Dictionary of rendered query targetsvariables
: Dictionary of rendered dashboard variablesinputs
: Dictionary of rendered datasource inputspanels
: Dictionary of rendered panelsrows
: Dictionary of rendered row panelsdashboards
: Dictionary of rendered dashboards
The builder.py
script has been tested with the popular Node Exporter Full dashboard v41 from Grafana.com. This dashboard contains complex panels, variables, and queries that demonstrate the tool's capability to handle real-world Grafana dashboards.
❯ python3 builder.py convert --input node-exporter-full.json
[✓] Converted node-exporter-full.json -> templates
❯ python3 builder.py build
***
[✓] Saved output/json/prometheus/dashboards/node_exporter_full.json
[✓] Saved output/yaml/prometheus/dashboards/node_exporter_full.yaml
❯ diff node-exporter-full.json output/json/prometheus/dashboards/node_exporter_full.json
# Download the Node Exporter Full dashboard
curl -o node-exporter-full.json "https://grafana.com/api/dashboards/1860/revisions/41/download"
# Convert to templates
python3 builder.py convert --input node-exporter-full.json --templates templates
# Build back to dashboard
python3 builder.py build --config config.yml --templates templates --output output
Generated dashboards are organized by format, datasource, and target type:
output/
├── json/
│ ├── prometheus/
│ │ └── dashboards/
│ │ └── system_overview.json
│ └── influxdb/
│ └── dashboards/
│ └── system_overview.json
└── yaml/
├── prometheus/
│ └── dashboards/
│ └── system_overview.yaml
└── influxdb/
└── dashboards/
└── system_overview.yaml
- Create a query target template (
templates/01_targets/cpu_usage.yml.j2
):
expr: 'cpu_usage{@{ prom_labels(labels) }@}'
datasource: "@{ datasource }@"
refId: "A"
- Create a panel template (
templates/02_panels/cpu_panel.yml.j2
):
title: "CPU Usage"
type: "graph"
targets:
- @{ targets["cpu_usage"] | to_nice_yaml | indent(4, false) }@
- Create a dashboard template (
templates/04_dashboards/system.yml.j2
):
title: "System Dashboard - @{ datasource }@"
panels:
- @{ panels["cpu_panel"] | to_nice_yaml | indent(4, false) }@
- Build the dashboard:
python3 builder.py build
This project is licensed under the MIT License. See the LICENSE file for details.
- Template syntax errors: Ensure you're using
@{ }@
delimiters, not{{ }}
- Missing templates: Check that all referenced templates exist in the correct directories
- YAML parsing errors: Validate your YAML syntax in template files
- Permission errors: Ensure the output directory is writable
Add debug prints to see what's being processed:
# Add to builder.py for debugging
print(f"Processing template: {template_path}")
print(f"Context: {template_context}")
- v0.1.0: Initial release with basic template building and conversion features
- Tested with Node Exporter Full dashboard v41
For issues and questions, please open an issue on the project repository.