Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources.Ā
Grafana connects with every possible data source, commonly referred to as databases such asĀ _Graphite, Prometheus, Influx DB, ElasticSearch, MySQL, PostgreSQLĀ _etc. Grafana being an open source solution also enables us to write plugins from scratch for integration with several different data sources. The tool helps us study, analyse & monitor data over a period of time, technically calledĀ time series analytics.
Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.
In this guide we will learn how to install and set up Grafana in Ubuntu 20.04 using Ansible.
Related content:
Table of Content
- Ensuring that the OS is updated
- Installing required packages
- Add the Grafana GPG key and APT repository
- Optional step: Installing and setting up Nginx as a proxy
1. Ensure that the OS is up to date
Before proceeding, let us ensure that we have the latest packages. Use these tasks to update the repositories and upgrade OS packages.
- name: Update apt repo and cache on all Ubuntu box
apt:
update_cache: yes
force_apt_get: yes
cache_valid_time: 3600
- name: Upgrade all packages on servers
apt:
upgrade: dist
force_apt_get: yes
2. Install required packages
Let us also install some packages that will be needed in the operations that we do later.
- name: Install required packages
apt:
name:
- gnupg2
- curl
- software-properties-common
state: present
3. Add the Grafana GPG key and APT repository
Since Grafana is not available in the default repositories, we will set up Grafana OSS ones. This task will add the gpg key and the apt repository.
- name: Add the Grafana GPG key and APT repository
shell: |
curl https://packages.grafana.com/gpg.key | sudo apt-key add -
add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
args:
warn: no
4. Install the grafana package
Once the repository is set up, we can install the grafana package. We are updating the cache before doing the install since we added a new repository.
- name: Install the grafana package
apt:
name: grafana
state: present
update_cache: yes
notify:
- Start grafana
- Enable grafana
5. Optional step: Installing and setting up Nginx as a proxy
To access Grafana from a domain, let us install Nginx and set up a virtual host. The site will be available through url grafana.citizix.com.
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
notify: Start and Enable Nginx
- name: Create grafana nginx config file
copy:
dest: /etc/nginx/conf.d/grafana.conf
mode: 0755
content: |
server {
listen 80;
server_tokens off;
client_max_body_size 10M;
server_name grafana.citizix.com;
ignore_invalid_headers off;
if ($host !~* ^(grafana.citizix.com)$ ) {
return 444;
}
location / {
send_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_connect_timeout 600;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:3000;
}
}
6. Setting up handlers
Handlers will be triggered after the tasks are done. We will use handlers to start and enable Grafana and Nginx.
- name: Start grafana
systemd:
name: grafana-server
state: started
- name: Enable grafana
systemd:
name: grafana-server
enabled: yes
- name: Start and Enable Nginx
systemd:
name: nginx
state: started
enabled: yes
7. Full code
This is the final code. I have this saved to a file grafana.yaml
.
---
- name: Install and set up grafana on Ubuntu 20.04
hosts: ubuntusrv
become: yes
gather_facts: False
tasks:
- name: Update apt repo and cache on all Ubuntu box
apt:
update_cache: yes
force_apt_get: yes
cache_valid_time: 3600
- name: Upgrade all packages on servers
apt:
upgrade: dist
force_apt_get: yes
- name: Install required packages
apt:
name:
- gnupg2
- curl
- software-properties-common
- name: Add the Grafana GPG key
shell: |
curl https://packages.grafana.com/gpg.key | sudo apt-key add -
add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
args:
warn: no
- name: Install the grafana package
apt:
name: grafana
state: present
update_cache: yes
notify:
- Start grafana
- Enable grafana
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
notify: Start and Enable Nginx
- name: Create grafana nginx config file
copy:
dest: /etc/nginx/conf.d/grafana.conf
mode: 0755
content: |
server {
listen 80;
server_tokens off;
client_max_body_size 10M;
server_name grafana1.citizix.com;
ignore_invalid_headers off;
if ($host !~* ^(grafana1.citizix.com)$ ) {
return 444;
}
location / {
send_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_connect_timeout 600;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:3000;
}
}
handlers:
- name: Start grafana
systemd:
name: grafana-server
state: started
- name: Enable grafana
systemd:
name: grafana-server
enabled: yes
- name: Start and Enable Nginx
systemd:
name: nginx
state: started
enabled: yes
8. Creating a hosts file
Hosts file will define connection to our server. In the same directory where the playbook is located, add this file, saved as hosts.yaml
.
all:
hosts:
ubuntusrv:
ansible_ssh_host: 10.2.11.9
ansible_ssh_user: ubuntu
ansible_ssh_private_key_file: ~/.ssh/admin.pem
9. Running the playbook
To run the playbook we need ansible installed locally. If you do not have ansible installed, use this pip command:
pip install -U ansible
We can then use ansible playbook to run the tasks:
ansible-playbook -i hosts.yaml grafana.yaml -vv
This should go through the tasks provisioning the server. When done, Grafana will be available in the URL you set up. You will need to add a DNS A record for the server for the domain to work.
Conclusion
In this guide we were able to use ansible to set up Grafana on an Ubuntu 20.04 server. We also installed Nginx and set up a virtual host to serve the site using a subdomain.