Since the beginning of the Covdi-19 epidemic, lot of us got much more time at home and it was a “good” occasion to work on projects we had put aside. That is my case, especially with those Raspberry Pi 4 which were left untouched for too long!
So I decided to gather some projects and topics which I wanted to invest time:
Install a TIG stack on Raspberry Pi
Using CSV plug-in for Telegraf
Continuing to work with Ansible
Exploring the new Grafana major version (7.0)
Visualize French Covid-19 data made available by the government
They are processed by Telegraf, then recorded in an InfluxDB database to be vizualized in Grafana.
This article is not intended to explain in detail how an Ansible Playbook should work, but rather to be a good example following my first article on the subject. So for those who feel a little lost with what will follow, don’t panic, we will have the opportunity to come back to it in detail in a future article.
Prerequisites
You will need a Raspberry Pi, if you don’t have one available, you can use an Ubuntu VM.
I’ve gotten used to not edit the default inventory file (ex /etc/ansible/hosts) and it is a good practice. I would rather create a dedicated file for the inventory, it can help avoid mistakes.
Then we can check the node access:
1
ansible pi-tig -i inventory.ini -m ping -u ubuntu
ubuntu is the default user created for the Ubuntu image on Raspberry Pi. If you have chosen to install it on a VM, you will have to replace the username.
Because the main goal of the project is to stay simple and readable, all the variables are set in a single group_vars\all.yml file. This way, they will be automatically imported during the Playbook execution.
There are very few variables in this project, again, it is on purpose to make it easier to understand. We may use more of them, and it could be a good exercise if you start with Ansible to try to add some.
Playbook
As is often the case with Ansible, there are several options when it comes to chose a Playbook structure. To make it easier to understand, I chose to do a single Playbook with 4 Plays in it. There were other possibilities, like:
A single Play: as all three components are deployed on the same node with the same user and with a unique variable file.
The Playbook would be less readable though
Roles: creation of three roles for Grafana, InfluxDB and Telegraf.
This may lead in a too complex project compare with the simplicity of the actions needed.
We will have the opportunity to deal with roles in a later article regarding the TIG stack !
One of the biggest advantage of Ansible is that I don’t really need to explain what the tasks* are doing 😇
At the time where I wrote this article, InfluxData (the company behind InfluxDB and Telegraf) are not providing sources for the version 20.04 of Ubuntu, that’s why I specified the bionic version to be used for now. I have left, in line 18, the right way to get the current version of Ubuntu through the Ansible Facts.
- name:InfluxDBhosts:pi-tigremote_user:ubuntubecome:truegather_facts:falsetasks:- name:Install InfluxDBapt:update_cache:yespkg:influxdb- name:Service started and enabledservice:name:influxdbenabled:yesstate:started- name:Wait influxdb service to be readywait_for:timeout:20- name:Check database presenceuri:url:"http://127.0.0.1:8086/query"method:POSTbody:'q=SHOW DATABASES'register:response- name:Create databaseuri:url:"http://127.0.0.1:8086/query"method:POSTbody:'q=CREATE DATABASE "{{ influxdb_dbname }}"'register:responsechanged_when:response.status == 200when:"influxdb_dbname not in response.json | json_query('results[0].series[0].values[*][0]')"
I am not using the Ansible InfluxDB modules because they do not seem maintain anymore, the last supported version is 1.2.4 and we are now using the 1.8 version! We are using the Rest API instead which mean we don’t need any additional python library to be installed.
- name:Telegrafhosts:pi-tigremote_user:ubuntubecome:truegather_facts:falsetasks:- name:Install Telegrafapt:update_cache:yespkg:telegraf- name:Service started and enabledservice:name:telegrafenabled:yesstate:started- name:Copy the telegraf configuration filecopy:src:"files/telegraf.conf"dest:"/etc/telegraf/telegraf.conf"mode:0644force:yesnotify:restart telegraf- name:Copy covid19 data CSV filecopy:src:"{{ item}}"dest:"/tmp/covid19.csv"mode:0644force:yeswith_fileglob:- "files/donnees-hospitalieres-covid19-*.csv"notify:restart telegraf handlers:- name:restart telegrafservice:name:telegrafstate:restarted
Here we are copying the Telegraf configuration file alongside the data file. The CSV data transformation is explained inside the configuration file.
You can replace the data file with a more recent one. The Playbook is using with_fileglob with a wildcard to match a different source file if needed.
Once Telegraf have sent the data into InfluxDB, you can safely stop and disable the service. If you don’t, it will try to send them again each 5min (which can be convenient for update)
Grafana
Then we install Grafana and we can import the dashboard: