Grafana Ansible Role
Overview
The grafana_setup role installs and configures Grafana on Debian/Ubuntu hosts, provisions Prometheus as the default datasource, deploys a file-based dashboard set, configures LDAP authentication, and connects Grafana to a PostgreSQL backend.
The primary deployment playbook is playbooks/grafana/deploy_grafana.yml, which applies:
globalgrafana_setup
The role expects supporting infrastructure to exist or be managed through companion playbooks:
- PostgreSQL database creation:
playbooks/grafana/create_db.yml - PostgreSQL backup:
playbooks/grafana/backup_db.yml - PostgreSQL restore:
playbooks/grafana/restore_db.yml
Role Task Flow
The role runs the following tasks in order:
- Update
pg_hba.confon the PostgreSQL server to allow password access from the Grafana host IP. - Update
pg_hba.confon the PostgreSQL server to allow password access from the configured database host IP. - Install required OS packages:
adduserlibfontconfig1musl
- Download and extract the Grafana tarball into
/opt/. - Create
/opt/grafanaas a symlink to the versioned install directory. - Create the Grafana data directory.
- Create the Grafana log directory.
- Create the Grafana plugins directory.
- Create the Grafana PID directory.
- Create the Grafana provisioning root directory.
- Create the Grafana provisioning plugins directory.
- Create the Grafana provisioning datasources directory.
- Create the Grafana provisioning dashboards directory.
- Create the Grafana dashboards directory.
- Render the Prometheus datasource provisioning file.
- Render the dashboard provider provisioning file.
- Copy the NGINX Prometheus exporter dashboard.
- Copy the observability landing dashboard.
- Copy the node exporter overview dashboard.
- Copy the node exporter detail dashboard.
- Create the Grafana configuration directory.
- Render
defaults.ini. - Render
ldap.toml. - Render the Grafana environment defaults file.
- Recursively enforce ownership on the Grafana home directory tree.
- Render the systemd service unit for Grafana.
- Enable and start the Grafana service.
Handlers
The role uses these handlers:
Restart GrafanaRestart PostgreSQLReload systemd daemon
Grafana restarts are triggered when provisioning files, dashboard JSON files, LDAP config, environment defaults, the systemd unit, or defaults.ini change.
Installation and Layout
Important role defaults and paths:
- Grafana home:
/data/grafana - Logs:
/data/grafana/logs - Plugins:
/data/grafana/plugins - Config:
/data/grafana/conf/defaults.ini - LDAP config:
/data/grafana/conf/ldap.toml - Provisioning root:
/data/grafana/provisioning - Provisioned dashboards directory:
/data/grafana/dashboards - Systemd service unit:
/usr/lib/systemd/system/grafana-server.service
Grafana is installed from the upstream tarball, not from a distro package. The version and edition are controlled through role defaults.
Dashboard Generation and Provisioning
Dashboard Source Model
Dashboards in this role are file-based JSON dashboards stored in the role under:
roles/grafana_setup/files/dashboards/
Current dashboard files include:
nginx-prometheus-exporter.dashboard.jsonobservability-landing.dashboard.jsonnode-exporter-overview.dashboard.jsonnode-exporter-node-detail.dashboard.json
These files are copied directly to the target host under:
/data/grafana/dashboards
Dashboard Provider Configuration
The role renders a Grafana dashboard provisioning file from provisioning-dashboards.yml.j2 with:
apiVersion: 1- provider type
file - folder name controlled by
grafana_setup_dashboard_folder - scan path set to
grafana_setup_dashboards_dir - refresh interval controlled by
grafana_setup_dashboard_update_interval_seconds - UI editability controlled by
grafana_setup_dashboard_allow_ui_updates
This means Grafana continuously scans the dashboard directory and imports or refreshes dashboards from disk.
Home Dashboard
The role sets Grafanaβs default home dashboard through defaults.ini using:
default_home_dashboard_path =
In the current configuration, that path points to:
/data/grafana/dashboards/observability-landing.dashboard.json
That makes the observability landing dashboard the expected Grafana home dashboard after deployment and restart.
Observability Landing Dashboard
The observability landing dashboard is intended to provide a quick operational overview of node state. It includes:
- A summary of tracked nodes
- Count of nodes up
- Count of nodes where the exporter is down
- Count of nodes where the VM is down
- A per-node status grid
Its node availability logic distinguishes between:
UpExporter DownVM Down
The distinction is based on Prometheus data using up{job="node"} together with recent presence of node_time_seconds, so the dashboard can tell the difference between a dead exporter and a dead VM.
Prometheus Integration
Prometheus is integrated through Grafana provisioning rather than manual UI setup.
Datasource Provisioning
The role renders prometheus.yml from provisioning-datasources.yml.j2 into:
/data/grafana/provisioning/datasources/prometheus.yml
The rendered datasource has these characteristics:
- datasource type:
prometheus - access mode:
proxy - marked as default datasource
- non-editable in the UI
- stable UID from
grafana_setup_prometheus_datasource_uid
By default the Prometheus URL is built from the first host in the prometheus inventory group:
http://:9090
Operational Effect
This gives Grafana an immediately available default Prometheus datasource on startup, which all provisioned dashboards can rely on without manual setup.
Why This Matters
Because both the datasource and the dashboards are provisioned from files:
- new Grafana hosts can be rebuilt consistently
- dashboards remain version-controlled
- datasource drift through manual UI edits is minimized
- dashboards referencing UID
prometheusremain stable across redeployments
PostgreSQL Backend
Grafana is configured to use PostgreSQL as its backend database.
Runtime Configuration
defaults.ini renders the following PostgreSQL connection settings:
- database type:
postgres - host:
: - database name: ``
- username: ``
- password: ``
- SSL mode:
disable
This backend stores Grafana application state such as:
- users
- dashboards and dashboard metadata
- folders
- permissions
- alerting metadata
- configuration state that Grafana persists internally
Database Deployment
Database deployment is handled separately from Grafana host deployment through:
playbooks/grafana/create_db.yml
That playbook runs on pgdb hosts and applies:
globalpython3autofs
It then imports roles/grafana_setup/tasks/database.yml.
The database task file performs these steps:
- Ensure PostgreSQL is installed.
- Start and enable PostgreSQL.
- Ensure
python3-psycopg2is installed for Ansible PostgreSQL modules. - Create the Grafana PostgreSQL user.
- Create the Grafana PostgreSQL database owned by that user.
- Create a
.pgpassentry for password-based access.
In addition, the main Grafana role updates PostgreSQL pg_hba.conf so the Grafana host can authenticate to PostgreSQL over the network.
Database Backup Process
Database backup is handled through:
playbooks/grafana/backup_db.yml
That playbook imports roles/grafana_setup/tasks/database_backup.yml on the pgdb host.
The backup workflow is:
- Ensure the backup directory exists.
- Run
pg_dumpin custom format (-Fc) against the Grafana database. - Store the output at
grafana_setup_backup_path. - Retain only the newest three backup files.
Default backup location pattern:
- base directory:
/grafana - filename prefix:
grafana_ - extension:
.sqlc
This produces timestamped PostgreSQL custom-format backups suitable for pg_restore.
Database Restore Process
Database restore is handled through:
playbooks/grafana/restore_db.yml
The restore process is intentionally split into three phases:
1. Stop Grafana
The playbook first stops the Grafana service on the grafana hosts.
2. Restore on PostgreSQL Host
It then imports roles/grafana_setup/tasks/database_restore.yml on the pgdb host.
That restore task file performs:
- If no explicit restore path is provided, search the backup directory for
.sqlcfiles. - Fail if no backups are available.
- Select the explicitly requested backup, or otherwise the newest available backup.
- Verify the selected backup file exists.
- Drop the existing Grafana database.
- Recreate the Grafana database owned by the Grafana DB user.
- Run
pg_restoreto load the backup.
3. Start Grafana Again
After restore completes, the playbook starts Grafana again on the grafana hosts.
Backend Operational Notes
This separation between application deployment and database lifecycle is useful because it allows:
- independent database creation
- controlled backups
- safer restore operations with the application stopped
- targeted execution against
pgdbrather than Grafana hosts
LDAP Authentication
LDAP is enabled and configured as part of the role.
Grafana LDAP Enablement
In defaults.ini, the role enables LDAP with:
[auth.ldap]enabled = trueconfig_file = /ldap.tomlallow_sign_up = trueskip_org_role_sync = false
That tells Grafana to load LDAP settings from the rendered ldap.toml file.
LDAP Configuration File
The role renders ldap.toml from roles/grafana_setup/templates/ldap.toml.j2 into:
/data/grafana/conf/ldap.toml
The template configures:
- LDAP server hostname
- LDAP port
- whether SSL is used
- bind DN
- bind password
- user search filter
- user search base DN
- group search filter
- group search base DN
- group-to-role mappings
- LDAP attribute mappings
LDAP Variables
Important LDAP defaults include:
grafana_setup_ldap_servergrafana_setup_ldap_portgrafana_setup_ldap_use_tlsgrafana_setup_ldap_bind_dngrafana_setup_ldap_searchdngrafana_setup_ldap_searchfiltergrafana_setup_ldap_group_dngrafana_setup_ldap_editor_groupgrafana_setup_ldap_viewer_groupgrafana_setup_ldap_group_searchfiltergrafana_setup_ldap_group_searchdngrafana_setup_ldap_verbose_logging
The bind password is stored separately in vault-backed role defaults and should not be documented in plaintext.
Group Mapping Behavior
The role maps LDAP groups to Grafana organization roles as follows:
grafana_setup_ldap_group_dnβAdmingrafana_setup_ldap_editor_groupβEditorgrafana_setup_ldap_viewer_groupβViewer
This means role assignment in Grafana is delegated to directory group membership rather than managed manually inside Grafana.
Attribute Mapping
The template maps standard LDAP attributes to Grafana user fields:
- username β
sAMAccountName - surname β
sn - email β
userPrincipalName - given name β
givenName
Operational Effect
With LDAP enabled:
- users authenticate against the configured directory
- Grafana can create users on login if needed
- organization role assignment follows LDAP group membership
- role sync remains active because
skip_org_role_sync = false
Configuration Files Rendered by the Role
The role renders these key files:
defaults.ini- Main Grafana configuration
- Includes database, LDAP enablement, dashboard home path, logging, and server settings
ldap.toml- LDAP server connection and role mapping rules
prometheus.yml- Provisioned Prometheus datasource
dashboards.yml- File-based dashboard provider
- environment defaults file
- Runtime environment variables for the systemd service
grafana-server.service- Systemd unit for Grafana
Restart and Change Behavior
The role notifies handlers when relevant files change.
A change to any of the following causes a Grafana restart:
- datasource provisioning
- dashboard provider provisioning
- dashboard JSON files
defaults.inildap.toml- Grafana environment defaults
- systemd unit file
A systemd daemon reload also occurs when the systemd service unit changes.
Suggested Deployment Sequence
A typical full lifecycle for this role is:
- Run
playbooks/grafana/create_db.ymlto create the PostgreSQL backend. - Run
playbooks/grafana/deploy_grafana.ymlto install and configure Grafana. - Verify Prometheus datasource provisioning and dashboard availability.
- Use
playbooks/grafana/backup_db.ymlfor routine backups. - Use
playbooks/grafana/restore_db.ymlfor controlled restore operations.
Summary
The grafana_setup role is structured around file-based, reproducible Grafana configuration:
- Grafana is installed from an upstream tarball.
- Prometheus is provisioned automatically as the default datasource.
- Dashboards are version-controlled JSON files imported from disk.
- The observability landing dashboard is configured as the default home dashboard.
- PostgreSQL is the persistent backend and has separate create, backup, and restore workflows.
- LDAP is enabled for centralized authentication and role mapping.