Ansible grafana_ds

datasources Grafana avec Ansible

2017-09-12

Created by Thierry Sallé / @tsalle

Ansible

  • Automatisation des deploiements
  • Sans agents (ssh)
  • Python
  • Powered By RedHat
  • Grosse communaute

API Grafana

documentation

http://grafana.company.com/api

  • GET : list datasources
  • POST : create datasource
  • PUT : update datasource
  • DELETE : delete datasource

grafana_ds


  Create/update/delete grafana datasources via API

Options (= is mandatory):

- access
        Access mode for this datasource
        (Choices: proxy, direct)[Default: proxy]
- basic_auth_password
        Datasource basic auth password, when basic auth is true
        [Default: (null)]
- basic_auth_user
        Datasource basic auth user.
        Setting this option with basic_auth_password will enable basic auth.
        [Default: (null)]
- database
        Name of the database for the datasource.
        This options is required when the ds_type is influxdb or elasticsearch (index name)
        [Default: (null)]
= ds_type
        Type of the datasource `graphite',`prometheus',`elasticsearch',`influxdb',`opentsdb'
        (Choices: graphite, prometheus, elasticsearch, influxdb, opentsdb)
- es_version
        Elasticsearch version (for ds_type = elasticsearch only)
        [Default: 5]
- grafana_api_key
        Grafana API key
        If set, grafana_user and grafana_password will be ignored
        [Default: (null)]
- grafana_password
        Grafana API password
        [Default: admin]
= grafana_url
        Grafana url

- grafana_user
        Grafana API user
        [Default: admin]
- is_default
        Make this datasource the default one
        [Default: False]
= name
        Name of the datasource

- org_id
        Grafana Organisation ID in which the datasource should be created
        Not used when grafana_api_key is set, because the grafana_api_key only belong to one organisation.
        [Default: 1]
- password
        Datasource password
        [Default: (null)]
- state
        Status of the datasource
        (Choices: present, absent)[Default: present]
- time_field
        Name of the time field in elasticsearch ds
        For example @timestamp
        [Default: timestamp]
- time_interval
        Minimum group by interval for influxdb or elasticsearch datasources
        for example '>10s'
        [Default: (null)]
- tls_ca_cert
        tls ca certificate for self signed certificates.
        only used when tls_client_cert and tls_client_key are set
        [Default: (null)]
- tls_client_cert
        client tls certificate.
        If tls_client_cert and tls_client_key are set, this will enable tls auth.
        begins with ----- BEGIN CERTIFICATE -----
        [Default: (null)]
- tls_client_key
        client tls private key
        befins with ----- BEGIN RSA PRIVATE KEY -----
        [Default: (null)]
- tsdb_resolution
        opentsdb time resolution (1 for second, 2 for millisecond)
        (Choices: 1, 2)[Default: 1]
- tsdb_version
        opentsdb version (1 for <= 2.1, 2 for ==2.2, 3 for ==2.3)
        (Choices: 1, 2, 3)[Default: 1]
= url
        Url of the datasource
- user
        Datasource login user for influxdb datasources
        [Default: (null)]
- validate_certs
        Validate or not grafana certificate
        [Default: (null)]
- with_credentials
        Whether credentials such as cookies or auth headers should be sent with cross-site requests.
        [Default: False]

parameter required default choices comments
name yes Name of the datasource
grafana_url yes Grafana url
grafana_user no admin Grafana API user
grafana_password no admin Grafana API password
grafana_api_key no Grafana API key. If set, grafana_user and grafana_password will be ignored
org_id no Grafana Organisation ID in which the datasource should be created. Not used when grafana_api_key is set, because the grafana_api_key only belong to one organisation.
ds_type tes graphite, prometheus, elasticsearch, influxdb, opentsdb Type of the datasource `graphite',`prometheus',`elasticsearch',`influxdb',`opentsdb'
url yes Url of the datasource
access yes proxy proxy, direct Access mode for this datasource
is_default no False True, False Make this datasource the default one
basic_auth_user no Datasource basic auth user. Setting this option with basic_auth_password will enable basic auth.
basic_auth_password no Datasource basic auth password, when basic auth is true
with_credentials no False True, False Whether credentials such as cookies or auth headers should be sent with cross-site requests.
database no Name of the database for the datasource. This options is required when the ds_type is influxdb or elasticsearch (index name)
es_version no 5 2, 5 Elasticsearch version (for ds_type = elasticsearch only)
state no present present, absent Status of the datasource
tls_client_cert no client tls certificate. If tls_client_cert and tls_client_key are set, this will enable tls auth.
tls_client_key no client tls private key
tls_ca_cert no tls ca certificate for self signed certificates. only used when tls_client_cert and tls_client_key are set.
time_field no timestamp Name of the time field in elasticsearch ds. For example @timestamp
time_interval no Minimum group by interval for influxdb or elasticsearch datasources. For example '10s'
user no Datasource login user for influxdb datasources
password no Datasource password
validate_certs no True, False Validate or not grafana certificate
tsdb_resolution no 1 1, 2 opentsdb time resolution (1 for second, 2 for millisecond)
tsdb_version no 1 1, 2, 3 opentsdb version (1 for <= 2.1, 2 for ==2.2, 3 for ==2.3)

Exemples


---
- hosts: grafana
  tasks:
    - name: creation datasources grafana
      connection: local
      grafana_ds:
        name: "ansible_elasticsearch_ds"
        grafana_url: "https://grafana.company.com"
        grafana_user: "admin"
        grafana_password: "{{ grafana_admin_password }}"
        org_id: "1"
        ds_type: "elasticsearch"
        url: "https://elasticsearch.company.com:9200"
        database: "my_index_*"
        time_interval: "1m"
        basic_auth_user: "es_user"
        basic_auth_password: "{{ es_grafana_password }}"
        validate_certs: false
        state: present

---
- hosts: grafana
  tasks:
    - name: suppression datasources grafana
      connection: local
      grafana_ds:
        name: "old_datasource_unused"
        grafana_url: "https://grafana.company.com"
        grafana_user: "admin"
        grafana_password: "{{ grafana_admin_password }}"
        url: "https://elasticsearch.company.com:9200"
        org_id: "1"
        state: absent

---
- hosts: grafana
  tasks:
    - name: creation datasource influxdb
      connection: local
      grafana_ds:
        name: "ansible_influxdb_datasource"
        grafana_url: "https://grafana.company.com"
        grafana_user: "admin"
        grafana_password: "{{ grafana_admin_password }}"
        org_id: "1"
        ds_type: "influxdb"
        url: "https://influxdb.company.com:8086"
        database: "telegraf"
        validate_certs: false
        state: present


---
- hosts: grafana
  tasks:
    - name: creation datasources grafana
      connection: local
      grafana_ds:
        name: "{{ item[0].name }}"
        grafana_url: "{{ grafana_url }}"
        grafana_user: "admin"
        grafana_password: "{{ grafana_admin_password }}"
        org_id: "{{ item[1] }}"
        ds_type: "{{ item[0].type }}"
        url: "{{ item[0].url }}"
        database: "{{ item[0].index }}"
        time_interval: "{{ item[0].group_by_time }}"
        basic_auth_user: "{{ item[0].user }}"
        basic_auth_password: "{{ item[0].pass }}"
        interval: "{{ item[0].interval | default('') }}"
        tls_client_cert: "{% if item[0].tls_auth is defined and item[0].tls_auth|bool %}{{ tls_certificate_cert }}{% endif %}"
        tls_client_key: "{% if item[0].tls_auth is defined and item[0].tls_auth|bool %}{{  tls_certificate_key }}{% endif %}"
        tls_ca_cert: "{% if item[0].tls_auth is defined and item[0].tls_auth|bool %}{{  tls_ca_certificate }}{% endif %}"
      with_nested:
        - "{{ grafana_datasources }}"
        - [ 1, 2 ]
      loop_control:
        label: "org id: {{ item[1] }}, name: {{ item[0].name }}"
      tags: [grafana_ds]

---
grafana_datasources:
  - name: "foo"
    url: "http://elastic.company.com:9200"
    type: "elasticsearch"
    user: "es_user"
    password: "{{ es_password }}"
    group_by_time: "1m"
    index: "data_app_foo*"
    tls_auth: True

  - name: "bar"
    url: "http://elastic.company.com:9200"
    type: "elasticsearch"
    user: "es_user"
    password: "{{ es_password }}"
    group_by_time: "1m"
    index: "data_app_bar*"
    tls_auth: True

Merci..