summaryrefslogtreecommitdiffstats
path: root/roles
diff options
context:
space:
mode:
authorAhmed Abdelhalim <[email protected]>2025-09-19 14:51:56 +0200
committerAhmed Abdelhalim <[email protected]>2025-09-19 14:51:56 +0200
commit60f9e792540cd3d56fb0329bf30fb4eb9ad9cf64 (patch)
treeb79d9f5044441bd2a27ae922e2e87c6b52043000 /roles
parent0460ecb3cfefc67a79d46d6d8f56676d77c9f507 (diff)
Add Grafana role
Diffstat (limited to 'roles')
-rw-r--r--roles/grafana/defaults/main.yml31
-rw-r--r--roles/grafana/handlers/main.yml6
-rw-r--r--roles/grafana/meta/argument_specs.yml84
-rw-r--r--roles/grafana/meta/main.yml18
-rw-r--r--roles/grafana/tasks/install-archlinux.yml6
-rw-r--r--roles/grafana/tasks/install-debian.yml25
-rw-r--r--roles/grafana/tasks/main.yml84
-rw-r--r--roles/grafana/templates/dashboards/dashboards.yml.j213
-rw-r--r--roles/grafana/templates/datasources/prometheus.yml.j212
-rw-r--r--roles/grafana/templates/grafana.ini.j232
-rw-r--r--roles/grafana/vars/archlinux.yml2
-rw-r--r--roles/grafana/vars/debian.yml2
12 files changed, 315 insertions, 0 deletions
diff --git a/roles/grafana/defaults/main.yml b/roles/grafana/defaults/main.yml
new file mode 100644
index 00000000..4e0e5614
--- /dev/null
+++ b/roles/grafana/defaults/main.yml
@@ -0,0 +1,31 @@
+---
+grafana_port: 3000
+grafana_admin_user: "admin"
+grafana_admin_password: "changeme"
+grafana_database_type: "sqlite3"
+
+grafana_datasources:
+ - name: "prometheus"
+ uid: "prometheus"
+ url: "http://localhost:9090"
+ default: true
+
+grafana_dashboards:
+ - id: 1860
+ revision: 37
+ name: "node-exporter-full"
+ datasource_mappings:
+ - key: "000000001"
+ value: "prometheus"
+ - id: 3662
+ revision: 2
+ name: "prometheus-overview"
+ datasource_mappings:
+ - key: "${DS_THEMIS}"
+ value: "prometheus"
+ - id: 11074
+ revision: 9
+ name: "node-exporter-prometheus"
+ datasource_mappings:
+ - key: "${DS__VICTORIAMETRICS}"
+ value: "prometheus"
diff --git a/roles/grafana/handlers/main.yml b/roles/grafana/handlers/main.yml
new file mode 100644
index 00000000..d366343b
--- /dev/null
+++ b/roles/grafana/handlers/main.yml
@@ -0,0 +1,6 @@
+---
+- name: "Restart grafana"
+ become: true
+ ansible.builtin.systemd_service:
+ name: "{{ grafana_service_name }}"
+ state: "restarted"
diff --git a/roles/grafana/meta/argument_specs.yml b/roles/grafana/meta/argument_specs.yml
new file mode 100644
index 00000000..1baa0e14
--- /dev/null
+++ b/roles/grafana/meta/argument_specs.yml
@@ -0,0 +1,84 @@
+---
+argument_specs:
+ main:
+ short_description: "Install and configure Grafana with datasources and dashboards"
+ description: "Install and configure Grafana monitoring system with Prometheus datasources and community dashboards"
+ options:
+ grafana_port:
+ description: "Port for Grafana web interface"
+ type: "int"
+ default: 3000
+ grafana_admin_user:
+ description: "Grafana admin username"
+ type: "str"
+ default: "admin"
+ grafana_admin_password:
+ description: "Grafana admin password"
+ type: "str"
+ default: "changeme"
+ grafana_database_type:
+ description: "Database type for Grafana"
+ type: "str"
+ default: "sqlite3"
+ choices:
+ - "sqlite3"
+ - "mysql"
+ - "postgres"
+ grafana_datasources:
+ description: "List of datasources to provision"
+ type: "list"
+ elements: "dict"
+ default: []
+ options:
+ name:
+ description: "Name of the datasource"
+ type: "str"
+ required: true
+ uid:
+ description: "Unique identifier for the datasource to be used in dashboard configurations"
+ type: "str"
+ required: true
+ type:
+ description: "Type of datasource"
+ type: "str"
+ default: "prometheus"
+ url:
+ description: "URL of the datasource"
+ type: "str"
+ required: true
+ default:
+ description: "Whether this is the default datasource"
+ type: "bool"
+ default: false
+ grafana_dashboards:
+ description: "List of community dashboards to download and provision"
+ type: "list"
+ elements: "dict"
+ default: []
+ options:
+ id:
+ description: "Grafana.com dashboard ID"
+ type: "int"
+ required: true
+ revision:
+ description: "Dashboard revision number"
+ type: "int"
+ required: true
+ name:
+ description: "Local name for the dashboard file"
+ type: "str"
+ required: true
+ datasource_mappings:
+ description: "List of datasource variable mappings for the dashboard"
+ type: "list"
+ elements: "dict"
+ default: []
+ options:
+ key:
+ description: "Original datasource reference to replace"
+ type: "str"
+ required: true
+ value:
+ description: "Target datasource UID to use"
+ type: "str"
+ required: true
diff --git a/roles/grafana/meta/main.yml b/roles/grafana/meta/main.yml
new file mode 100644
index 00000000..d67d948d
--- /dev/null
+++ b/roles/grafana/meta/main.yml
@@ -0,0 +1,18 @@
+---
+dependencies: []
+galaxy_info:
+ author: "a14m"
+ description: "Install Grafana for monitoring"
+ company: "kartoffeln.work GmbH."
+ license: "MIT"
+ min_ansible_version: "2.18"
+ platforms:
+ - name: "ArchLinux"
+ versions:
+ - "all"
+ - name: "Ubuntu"
+ versions:
+ - "noble"
+ - name: "Debian"
+ versions:
+ - "bookworm"
diff --git a/roles/grafana/tasks/install-archlinux.yml b/roles/grafana/tasks/install-archlinux.yml
new file mode 100644
index 00000000..26e94004
--- /dev/null
+++ b/roles/grafana/tasks/install-archlinux.yml
@@ -0,0 +1,6 @@
+---
+- name: "Install grafana"
+ become: true
+ ansible.builtin.package:
+ name: "grafana"
+ state: "present"
diff --git a/roles/grafana/tasks/install-debian.yml b/roles/grafana/tasks/install-debian.yml
new file mode 100644
index 00000000..42eb44bf
--- /dev/null
+++ b/roles/grafana/tasks/install-debian.yml
@@ -0,0 +1,25 @@
+---
+- name: "Download Grafana GPG key"
+ become: true
+ ansible.builtin.get_url:
+ url: "https://apt.grafana.com/gpg.key"
+ dest: "/etc/apt/trusted.gpg.d/grafana.asc"
+ mode: "0644"
+
+- name: "Add Grafana repository"
+ become: true
+ ansible.builtin.apt_repository:
+ repo: "deb [signed-by=/etc/apt/trusted.gpg.d/grafana.asc] https://apt.grafana.com stable main"
+ state: "present"
+ filename: "grafana"
+
+- name: "Update package cache"
+ become: true
+ ansible.builtin.apt:
+ update_cache: true
+
+- name: "Install grafana"
+ become: true
+ ansible.builtin.package:
+ name: "grafana"
+ state: "present"
diff --git a/roles/grafana/tasks/main.yml b/roles/grafana/tasks/main.yml
new file mode 100644
index 00000000..1d8dceaf
--- /dev/null
+++ b/roles/grafana/tasks/main.yml
@@ -0,0 +1,84 @@
+---
+- name: "Include OS-specific variables"
+ ansible.builtin.include_vars: "{{ ansible_distribution | lower }}.yml"
+
+- name: "Include OS-specific installation tasks"
+ ansible.builtin.include_tasks: "install-{{ ansible_distribution | lower }}.yml"
+
+- name: "Create grafana group"
+ become: true
+ ansible.builtin.group:
+ name: "grafana"
+ system: true
+
+- name: "Create grafana user"
+ become: true
+ ansible.builtin.user:
+ name: "grafana"
+ group: "grafana"
+ system: true
+ shell: "/usr/sbin/nologin"
+ create_home: false
+
+- name: "Create grafana directories"
+ become: true
+ ansible.builtin.file:
+ path: "{{ item }}"
+ state: "directory"
+ owner: "grafana"
+ group: "grafana"
+ mode: "0755"
+ loop:
+ - "/etc/grafana"
+ - "/etc/grafana/provisioning"
+ - "/etc/grafana/provisioning/datasources"
+ - "/etc/grafana/provisioning/plugins"
+ - "/etc/grafana/provisioning/dashboards"
+ - "/var/lib/grafana"
+ - "/var/log/grafana"
+
+- name: "Template grafana configurations"
+ become: true
+ ansible.builtin.template:
+ src: "{{ item.src }}"
+ dest: "{{ item.dest }}"
+ owner: "root"
+ group: "grafana"
+ mode: "0640"
+ loop:
+ - src: "grafana.ini.j2"
+ dest: "/etc/grafana.ini"
+ - src: "datasources/prometheus.yml.j2"
+ dest: "/etc/grafana/provisioning/datasources/prometheus.yml"
+ - src: "dashboards/dashboards.yml.j2"
+ dest: "/etc/grafana/provisioning/dashboards/dashboards.yml"
+ notify: "Restart grafana"
+
+- name: "Provision Grafana community dashboards"
+ notify: "Restart grafana"
+ block:
+ - name: "Download Grafana community dashboards"
+ become: true
+ ansible.builtin.get_url:
+ url: "https://grafana.com/api/dashboards/{{ item.id }}/revisions/{{ item.revision }}/download"
+ dest: "/etc/grafana/provisioning/dashboards/{{ item.name }}.json"
+ owner: "grafana"
+ group: "grafana"
+ mode: "0644"
+ force: false
+ loop: "{{ grafana_dashboards }}"
+
+ - name: "Fix datasource references in downloaded dashboards"
+ become: true
+ ansible.builtin.replace:
+ path: "/etc/grafana/provisioning/dashboards/{{ item.0.name }}.json"
+ regexp: "{{ item.1.key | regex_escape }}"
+ replace: "{{ item.1.value }}"
+ loop: "{{ grafana_dashboards | subelements('datasource_mappings', skip_missing=True) }}"
+
+- name: "Start and enable grafana"
+ become: true
+ ansible.builtin.systemd_service:
+ name: "{{ grafana_service_name }}"
+ state: "started"
+ enabled: true
diff --git a/roles/grafana/templates/dashboards/dashboards.yml.j2 b/roles/grafana/templates/dashboards/dashboards.yml.j2
new file mode 100644
index 00000000..2893852b
--- /dev/null
+++ b/roles/grafana/templates/dashboards/dashboards.yml.j2
@@ -0,0 +1,13 @@
+apiVersion: 1
+
+providers:
+ - name: 'default'
+ orgId: 1
+ folder: ''
+ type: file
+ disableDeletion: false
+ updateIntervalSeconds: 10
+ allowUiUpdates: false
+ options:
+ path: /etc/grafana/provisioning/dashboards
+ foldersFromFilesStructure: false
diff --git a/roles/grafana/templates/datasources/prometheus.yml.j2 b/roles/grafana/templates/datasources/prometheus.yml.j2
new file mode 100644
index 00000000..1c60e9d9
--- /dev/null
+++ b/roles/grafana/templates/datasources/prometheus.yml.j2
@@ -0,0 +1,12 @@
+apiVersion: 1
+
+datasources:
+{% for ds in grafana_datasources %}
+ - name: "{{ ds.name }}"
+ type: "{{ ds.type | default('prometheus') }}"
+ uid: "{{ ds.uid }}"
+ access: proxy
+ url: "{{ ds.url }}"
+ isDefault: {{ ds.default | default(false) }}
+ editable: false
+{% endfor %}
diff --git a/roles/grafana/templates/grafana.ini.j2 b/roles/grafana/templates/grafana.ini.j2
new file mode 100644
index 00000000..7685bed2
--- /dev/null
+++ b/roles/grafana/templates/grafana.ini.j2
@@ -0,0 +1,32 @@
+[server]
+http_port = {{ grafana_port }}
+root_url = http://localhost:{{ grafana_port }}/
+
+[database]
+type = {{ grafana_database_type }}
+path = /var/lib/grafana/grafana.db
+
+[security]
+admin_user = {{ grafana_admin_user }}
+admin_password = {{ grafana_admin_password }}
+
+[users]
+allow_sign_up = false
+allow_org_create = false
+auto_assign_org = true
+auto_assign_org_role = Viewer
+
+[auth.anonymous]
+enabled = false
+
+[log]
+mode = console
+level = info
+
+[paths]
+data = /var/lib/grafana
+logs = /var/log/grafana
+provisioning = /etc/grafana/provisioning
+datasources = /etc/grafana/provisioning/datasources
+plugins = /etc/grafana/provisioning/plugins
+dashboards = /etc/grafana/provisioning/dashboards
diff --git a/roles/grafana/vars/archlinux.yml b/roles/grafana/vars/archlinux.yml
new file mode 100644
index 00000000..5fbb74df
--- /dev/null
+++ b/roles/grafana/vars/archlinux.yml
@@ -0,0 +1,2 @@
+---
+grafana_service_name: "grafana"
diff --git a/roles/grafana/vars/debian.yml b/roles/grafana/vars/debian.yml
new file mode 100644
index 00000000..6b8d0b21
--- /dev/null
+++ b/roles/grafana/vars/debian.yml
@@ -0,0 +1,2 @@
+---
+grafana_service_name: "grafana-server"