This guide provides a full Ansible playbook to install Grafana with repository setup and service management for Debian 10+, Ubuntu LTS, and RHEL 9+ compatible systems.
- name: Install Grafana
hosts: grafana
become: true
vars:
grafana_admin_user: "admin"
grafana_admin_password: "changeme"
grafana_http_port: 3000
tasks:
- name: Install required packages (Debian/Ubuntu)
apt:
name:
- apt-transport-https
- gnupg
- curl
state: present
update_cache: true
when: ansible_os_family == "Debian"
- name: Add Grafana GPG key (Debian/Ubuntu)
apt_key:
url: https://apt.grafana.com/gpg.key
state: present
when: ansible_os_family == "Debian"
- name: Add Grafana repository (Debian/Ubuntu)
apt_repository:
repo: "deb https://apt.grafana.com stable main"
state: present
filename: grafana
when: ansible_os_family == "Debian"
- name: Install required packages (RHEL)
dnf:
name:
- yum-utils
state: present
when: ansible_os_family == "RedHat"
- name: Add Grafana repository (RHEL)
yum_repository:
name: grafana
description: Grafana repository
baseurl: https://rpm.grafana.com
gpgcheck: 1
gpgkey: https://rpm.grafana.com/gpg.key
enabled: 1
when: ansible_os_family == "RedHat"
- name: Install Grafana
package:
name: grafana
state: present
notify: restart grafana
- name: Configure Grafana defaults
lineinfile:
path: /etc/grafana/grafana.ini
regexp: "{{ item.regexp }}"
line: "{{ item.line }}"
loop:
- { regexp: '^;?http_port', line: 'http_port = {{ grafana_http_port }}' }
- { regexp: '^;?admin_user', line: 'admin_user = {{ grafana_admin_user }}' }
- { regexp: '^;?admin_password', line: 'admin_password = {{ grafana_admin_password }}' }
notify: restart grafana
- name: Enable and start Grafana service
service:
name: grafana-server
state: started
enabled: true
handlers:
- name: restart grafana
service:
name: grafana-server
state: restarted
3000/tcp by defaultgrafana-server/etc/grafana/grafana.iniAny questions?
Feel free to contact us. Find all contact information on our contact page.