-
Notifications
You must be signed in to change notification settings - Fork 5
/
init.yml
116 lines (105 loc) · 4.23 KB
/
init.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
- name: configuring radon_master
hosts: master
gather_facts: true
sudo: yes
roles:
- radon_master
- hosts: monitor
tags:
- grafana
roles:
- { role: grafana, grafana_exec_vars_only: true }
tasks:
- name: Ensure grafana API Key directory exists
file:
path: "{{ playbook_dir }}/conf/keys"
state: directory
delegate_to: localhost
- name: Check grafana API Key list
uri:
url: "http://{{ ansible_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: "{{ playbook_dir }}/conf/keys/grafana_apikey.key"
register: grafana_apikey_file
delegate_to: localhost
- 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://{{ ansible_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://{{ ansible_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: "{{ playbook_dir }}/conf/keys/{{ item['item']['name'] }}.key"
content: "{{ item['json']['key'] }}"
backup: no
when: item['json'] is defined
with_items: "{{ new_api_keys['results'] }}"
delegate_to: localhost
- name: import grafana data source
shell: >
chdir={{ grafana_dir }}/data
warn=no
curl -q -X POST -d @data_source.json --header 'Content-Type: application/json'
"http://{{ grafana_admin_user }}:{{ grafana_admin_password }}@127.0.0.1:{{ grafana_port }}/api/datasources"
- name: import grafana dashboards - prepare config
delegate_to: localhost
template: src=grafana.dest.json.j2 dest={{ playbook_dir }}/scripts/dests.json
vars:
- ansible_become: false
- ansible_connection: local
- grafana_dest_config:
name: "{{ cluster_name | title }}"
url: "http://{{ansible_default_ipv4.address}}:{{ grafana_port }}/"
user: "{{ grafana_admin_user }}"
password: "{{ grafana_admin_password }}"
apikey: "{{ lookup('file', '{{ playbook_dir }}/conf/keys/grafana_apikey.key') }}"
datasource: "{{ cluster_name }}"
titles:
node: "{{ cluster_name | title }}-Node"
mysql: "{{cluster_name | title}}-Mysql"
radon: "{{cluster_name | title}}-Radon"
- name: import grafana dashboards - run import script
delegate_to: localhost
shell: >-
chdir={{ playbook_dir }}/scripts
python grafana-config-copy.py
vars:
- ansible_become: false
- ansible_connection: local