summaryrefslogtreecommitdiffstats
path: root/roles/grafana/tasks
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/grafana/tasks
parent0460ecb3cfefc67a79d46d6d8f56676d77c9f507 (diff)
Add Grafana role
Diffstat (limited to 'roles/grafana/tasks')
-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
3 files changed, 115 insertions, 0 deletions
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