4

How to add Grafana Data Source using Ansible

 3 years ago
source link: https://computingforgeeks.com/how-to-add-grafana-data-source-using-ansible/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
How to add Grafana Data Source using Ansible
Search

In this blog post, I’ll show you how you can easily add a Data Source to Grafana without using Grafana Web interface. Grafana supports various storage backends for your time series data, this is what is called Data Source.

Grafana Supported Data Sources

The following data sources are officially supported:

Each Data Source has a specific Query Editor that is customized for the features and capabilities that the particular Data Source exposes. I had done guides on Installation of Grafana if you don’t have one ready.

How to Install Grafana on Ubuntu 18.04 and Debian 9

Install Grafana and InfluxDB on CentOS 7

Once you have Grafana installed and working, you may need to install Data Source Server, this can be Prometheus, InfluxDB, Graphite.

Step 1: Install Ansible on your Linux

You need to to have ansible installed and working on your Linux system to use this method. You can easily install ansible on any Linux using python pip package manager.

Installing python-pip on Linux.

pip is a package management system used to install and manage software packages written in Python

Install pip on Ubuntu / Debian

sudo apt-get -y install python-pip

Install pip on CentOS

sudo yum -y install python-pip

Install pip on Arch Linux

sudo paman -S python-pip

Once pip is installed, upgrade it to the latest release

sudo pip install --upgrade pip

Step 2: Setup Ansible Environment

Create ansible base directory

mkdir -p ~/ansible

Change to ansible base directory and create a directory to store all your Ansible roles.

cd ~/ansible
mkdir roles

Under the roles directory, we will have tasks and defaults variables folder

mkdir -p roles/grafana-datasource/{tasks,defaults]

Step 3: Define Ansible Variables

Our Variables used to add datasource to Grafana will be defined on fileroles/defaults/main.yml. In this example, we will add an InfluxDB data source to Grafana. Below is our /defaults/main.yml which I’ll explain its contents in a bit.

$ cat defaults/main.yml
---
grafana_url: "http://192.168.50.3:3000"
grafana_user: admin
grafana_password: "GrafanaAdminPassword"
org_id: "1"

data_source:
  - name: ldap.example.com
    ds_type: "influxdb"
    url: "http://192.168.50.4:8086"
    user: "influx_user"
    password: "StrongPassword"

Where:

  • http://192.168.50.3:3000 is the URL of grafana. It is running on the default port 3000.
  • Grafana admin user is admin with the password GrafanaAdminPassword
  • The data source to be added is named ldap.example.com
  • Data source type is influxdb
  • http://192.168.50.4:8086 is the URL of an InfluxDB server
  • For an InfluxDB with authentication (Recommended), define username and password – influx_user and StrongPassword respectively.

Remember to replace the values with your correct ones.

Enabling Authentication on InfluxDB data source is defined on our InfluxDB and Grafana installation guideInstall Grafana and InfluxDB on CentOS 7, it applies to InfluxDB running on any other flavor of Linux.

Step 4: Create Ansible task

When all variables used for the creation of Data source are defined, proceed to create the task.

$ cat tasks/main.yml 
---
- name: Create influxdb datasource
  grafana_datasource:
    name: "{{ item.name }}"
    grafana_url: "{{ grafana_url }}"
    grafana_user: "{{ grafana_user }}"
    grafana_password: "{{ grafana_password }}"
    ds_type: "{{ item.ds_type }}"
    url: "{{ item.url }}"
    database: "{{ item.name }}"
    user: "{{ item.user }}"
    password: "{{ item.password }}"
    state: present
  with_items: "{{ data_source }}"

The task reference values defined on filedefaults/main.yml.

Step 5: Run Ansible Playbook

Change to root ansible directory and create playbook execution file.

cd ~/ansible/

Create a file with content like below

$ cat grafana-datasource.yml 
---
- name: Add data source to grafana
  hosts: localhost
  roles:
    - grafana-datasource

Finally, execute playbook by running:

$ ansible-playbook grafana-datasource.yml

Sample output:

# ansible-playbook grafana-datasource.yml

PLAY [Add data source to grafana] ********************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [localhost]

TASK [grafana-datasource : Create influxdb datasource] ***********************************************************************************************
changed: [localhost] => (item={u'url': u'http://192.168.50.4:8086', u'password': u'StrongPassword', u'ds_type': u'influxdb', u'name': u'ldap.example.com', u'user': u'influx_user'})

PLAY RECAP *******************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0   

That’s all. Confirm the data source on Grafana under sectionData Sources.

You can now add many data sources by editing name only for all InfluxDB data sources.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK