Ansible 2.5 grafana modules

 

At work, we needed to automatize grafana installation and grafana provisionning (datasources, plugins and dashboards).

So I’ve created 3 new ansible modules that will be released with the next version of ansible 2.5

The first module is grafana_datasource. If you have to create a lot of different datsources for your grafana instance in multiples organisations, I suggest you to use it.

with a single ansible task, you can create all your datasources. For example if I want to create multiple datasources :

 - name: create elasticsearch datasource
   grafana_datasource:
     name: "{{ item.name }}"
     grafana_url: "{{ grafana_url }}"
     grafana_user: "{{ grafana_user }}"
     grafana_password: "{{ grafana_password }}"
     ds_type: "{{ item.type }}"
     url: "{{ item.url }}"
     database: "{{ item.database }}"
     basic_auth_user: "{{ item.basic_auth_user | default('') }}"
     basic_auth_password: "{{ item.basic_auth_password | default('') }}"
     esVersion: "{{ item.es_version | default(5) }}"
     time_field": "{{ item.time_field | default('@timestamp') }}"
     state: present
  with_items: "{{ grafana_datasources }}"

where the grafana_datasources variable is :

grafana_datasources:
  - name: "es_index1"
    ds_type: "elasticsearch"
    url: "http://elasticsearch.aperogeek.fr:9200"
    database: "index_[YYYY.mm.dd]"
    basic_auth_user: "grafana"
    basic_auth_password: "{{ grafana_es_password }}"
    es_version: 56
  - name: "influxdb"
    ds_type: "influxdb"
    url: "http://elasticsearch.aperogeek.fr:9200"
    database: "telegraf"

 

The second module is grafana_plugin. With this one, you can automate the installation and the upgrade of all your grafana plugins. For example :

 - name: install - update Grafana piechart panel plugin
   grafana_plugin:
     name: grafana-piechart-panel
     version: latest

And the last one is grafana_dashboard. This one is very great because it allow you to import or backup all your existing dashboards.

  - name: import grafana dashboard foo
   grafana_dashboard:
     grafana_url: http://grafana.company.com
     grafana_api_key: XXXXXXXXXXXX
     state: present
     message: "updated by ansible"
     overwrite: true
     path: /path/to/dashboards/foo.json

 - name: export dashboard
   grafana_dashboard:
     grafana_url: http://grafana.company.com
     grafana_api_key: XXXXXXXXXXXX
     state: export
     slug: foo
     path: /path/to/dashboards/foo.json

Hope thoses new ansible modules will be usefull for someone 🙂

If you have some suggestion of missing feature in this modules, you can comment in this article or make a pull requests in ansible github repo.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.