|
1 | 1 | --- |
2 | | -- name: Check datasources list |
3 | | - uri: |
4 | | - url: "{{ grafana_url }}/api/datasources" |
5 | | - user: "{{ grafana_security.admin_user }}" |
6 | | - password: "{{ grafana_security.admin_password }}" |
7 | | - force_basic_auth: true |
8 | | - return_content: true |
9 | | - no_log: true |
10 | | - register: datasources |
| 2 | +- block: |
| 3 | + - name: Check datasources list (api) |
| 4 | + uri: |
| 5 | + url: "{{ grafana_url }}/api/datasources" |
| 6 | + user: "{{ grafana_security.admin_user }}" |
| 7 | + password: "{{ grafana_security.admin_password }}" |
| 8 | + force_basic_auth: true |
| 9 | + return_content: true |
| 10 | + no_log: true |
| 11 | + register: datasources |
11 | 12 |
|
12 | | -- name: Create grafana datasource |
13 | | - uri: |
14 | | - url: "{{ grafana_url }}/api/datasources" |
15 | | - user: "{{ grafana_security.admin_user }}" |
16 | | - password: "{{ grafana_security.admin_password }}" |
17 | | - force_basic_auth: true |
18 | | - method: POST |
19 | | - body_format: json |
20 | | - body: "{{ item | to_json }}" |
21 | | - with_items: "{{ grafana_datasources }}" |
22 | | - no_log: true |
23 | | - when: ((datasources['json'] | selectattr("name", "equalto", item['name'])) | list) | length == 0 |
| 13 | + - name: Create grafana datasources (api) |
| 14 | + uri: |
| 15 | + url: "{{ grafana_url }}/api/datasources" |
| 16 | + user: "{{ grafana_security.admin_user }}" |
| 17 | + password: "{{ grafana_security.admin_password }}" |
| 18 | + force_basic_auth: true |
| 19 | + method: POST |
| 20 | + body_format: json |
| 21 | + body: "{{ item | to_json }}" |
| 22 | + with_items: "{{ grafana_datasources }}" |
| 23 | + no_log: true |
| 24 | + when: ((datasources['json'] | selectattr("name", "equalto", item['name'])) | list) | length == 0 |
24 | 25 |
|
25 | | -- name: Create datasources file |
| 26 | + - name: Update grafana datasources (api) |
| 27 | + uri: |
| 28 | + url: "{{ grafana_url }}/api/datasources/{{ ( |
| 29 | + datasources['json'] | |
| 30 | + selectattr('name', 'equalto', item['name']) | |
| 31 | + map(attribute='id') | |
| 32 | + unique | |
| 33 | + join() |
| 34 | + ) }}" |
| 35 | + user: "{{ grafana_security.admin_user }}" |
| 36 | + password: "{{ grafana_security.admin_password }}" |
| 37 | + force_basic_auth: true |
| 38 | + method: PUT |
| 39 | + body_format: json |
| 40 | + body: "{{ item | to_json }}" |
| 41 | + with_items: "{{ grafana_datasources }}" |
| 42 | + no_log: true |
| 43 | + when: ((datasources['json'] | selectattr("name", "equalto", item['name'])) | list) | length != 0 |
| 44 | + when: not grafana_use_provisioning |
| 45 | + |
| 46 | +- name: Create/Update datasources file (provisioning) |
26 | 47 | copy: |
27 | 48 | dest: "/etc/grafana/provisioning/datasources/ansible.yml" |
28 | 49 | content: | |
29 | 50 | delete_datasources: [] |
30 | 51 | datasources: |
31 | 52 | {{ grafana_datasources | to_nice_yaml }} |
32 | 53 | backup: false |
| 54 | + become: true |
33 | 55 | notify: restart grafana |
| 56 | + when: grafana_use_provisioning |
0 commit comments