forked from pingcap/tidb-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_grafana_api_keys.yml
68 lines (61 loc) · 2.38 KB
/
create_grafana_api_keys.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
- name: Ensure grafana API Key directory exists
file:
path: "{{ grafana_api_keys_dir }}"
state: directory
delegate_to: localhost
- name: Check grafana API Key list
uri:
url: "http://{{ grafana_host }}:{{ grafana_port }}/api/auth/keys"
user: "{{ grafana_admin_user }}"
password: "{{ grafana_admin_password }}"
force_basic_auth: yes
return_content: yes
register: existing_api_keys
- name: Check grafana API Key file existed
stat:
path: "{{ grafana_api_keys_dir }}/grafana_apikey.key"
register: grafana_apikey_file
- set_fact:
apikey_id: "{{ item }}"
with_items: "{{ existing_api_keys.json|json_query(apikey_id_query) }}"
vars:
apikey_id_query: "[?name=='grafana_apikey'].id"
when:
- ((existing_api_keys['json'] | selectattr("name", "equalto", "grafana_apikey")) | list) | length == 1
- grafana_apikey_file.stat.exists == False
- debug:
var: apikey_id
when:
- ((existing_api_keys['json'] | selectattr("name", "equalto", "grafana_apikey")) | list) | length == 1
- grafana_apikey_file.stat.exists == False
- name: Delete grafana API Key when grafana API Key file is missing
uri:
url: "http://{{ grafana_host }}:{{ grafana_port }}/api/auth/keys/{{ apikey_id }}"
user: "{{ grafana_admin_user }}"
password: "{{ grafana_admin_password }}"
force_basic_auth: yes
method: DELETE
when:
- ((existing_api_keys['json'] | selectattr("name", "equalto", "grafana_apikey")) | list) | length == 1
- grafana_apikey_file.stat.exists == False
- name: Create grafana API Key
uri:
url: "http://{{ grafana_host }}:{{ grafana_port }}/api/auth/keys"
user: "{{ grafana_admin_user }}"
password: "{{ grafana_admin_password }}"
force_basic_auth: yes
method: POST
body_format: json
body: "{{ item | to_json }}"
with_items: "{{ grafana_api_keys }}"
when: (((existing_api_keys['json'] | selectattr("name", "equalto", item['name'])) | list) | length == 0) or (((existing_api_keys['json'] | selectattr("name", "equalto", "grafana_apikey")) | list) | length == 1 and grafana_apikey_file.stat.exists == False)
register: new_api_keys
- name: Create grafana API key file
become: no
copy:
dest: "{{ grafana_api_keys_dir }}/{{ item['item']['name'] }}.key"
content: "{{ item['json']['key'] }}"
backup: no
when: item['json'] is defined
with_items: "{{ new_api_keys['results'] }}"
delegate_to: localhost