Skip to content

Commit b70f77e

Browse files
committed
provide toplevel 'configure' task file
1 parent b8a135b commit b70f77e

File tree

4 files changed

+189
-186
lines changed

4 files changed

+189
-186
lines changed

tasks/configure.yml

Lines changed: 97 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,107 @@
11
---
2-
- name: Ensure grafana configuration directories exist
3-
file:
4-
path: "{{ item }}"
5-
state: directory
6-
owner: root
7-
group: grafana
8-
with_items:
9-
- "/etc/grafana"
10-
- "/etc/grafana/datasources"
11-
- "/etc/grafana/provisioning"
12-
- "/etc/grafana/provisioning/datasources"
13-
- "/etc/grafana/provisioning/dashboards"
14-
- "/etc/grafana/provisioning/notifiers"
15-
- "/etc/grafana/provisioning/plugins"
2+
- name: Gather variables for each operating system
3+
include_vars: "{{ item }}"
4+
with_first_found:
5+
- "{{ ansible_distribution | lower }}-{{ ansible_distribution_version | lower }}.yml"
6+
- "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version | lower }}.yml"
7+
- "{{ ansible_os_family | lower }}-{{ ansible_distribution_major_version | lower }}.yml"
8+
- "{{ ansible_distribution | lower }}.yml"
9+
- "{{ ansible_os_family | lower }}.yml"
10+
tags:
11+
- grafana_install
12+
- grafana_configure
13+
- grafana_datasources
14+
- grafana_notifications
15+
- grafana_dashboards
1616

17-
- name: Create grafana main configuration file
18-
template:
19-
src: grafana.ini.j2
20-
dest: /etc/grafana/grafana.ini
21-
owner: root
22-
group: grafana
23-
mode: 0640
24-
no_log: true
25-
notify: restart grafana
17+
- include: preflight.yml
18+
tags:
19+
- grafana_configure
20+
- grafana_datasources
21+
- grafana_notifications
22+
- grafana_dashboards
2623

27-
- name: Create grafana LDAP configuration file
28-
template:
29-
src: ldap.toml.j2
30-
dest: "{{ grafana_auth.ldap.config_file | default('/etc/grafana/ldap.toml') }}"
31-
owner: root
32-
group: grafana
33-
mode: 0640
34-
when:
35-
- "'ldap' in grafana_auth"
36-
- "'enabled' not in grafana_auth.ldap or grafana_auth.ldap.enabled"
37-
no_log: true
38-
notify: restart grafana
24+
- include: configure.yml
25+
become: true
26+
tags:
27+
- grafana_configure
3928

40-
- name: Create grafana directories
41-
file:
42-
path: "{{ item }}"
43-
state: directory
44-
mode: 0755
45-
owner: "grafana"
46-
group: "grafana"
47-
with_items:
48-
- "{{ grafana_logs_dir }}"
49-
- "{{ grafana_data_dir }}"
50-
- "{{ grafana_data_dir }}/dashboards"
51-
- "{{ grafana_data_dir }}/plugins"
29+
- include: plugins.yml
30+
when: grafana_plugins != []
31+
tags:
32+
- grafana_configure
5233

53-
- block:
54-
- name: Create grafana socket directory
55-
file:
56-
path: "{{ grafana_server.socket | dirname }}"
57-
state: directory
58-
mode: 0775
59-
owner: "grafana"
60-
group: "grafana"
34+
- name: Restart grafana before configuring datasources and dashboards
35+
meta: flush_handlers
36+
tags:
37+
- grafana_install
38+
- grafana_configure
39+
- grafana_datasources
40+
- grafana_notifications
41+
- grafana_dashboards
42+
- grafana_run
6143

62-
- name: Ensure grafana socket directory created on startup
63-
template:
64-
src: tmpfiles.j2
65-
dest: /etc/tmpfiles.d/grafana.conf
66-
owner: root
67-
group: root
68-
mode: 0644
44+
- name: Wait for grafana to start (http/s)
45+
wait_for:
46+
host: "{{ grafana_address }}"
47+
port: "{{ grafana_port }}"
6948
when:
70-
- grafana_server.protocol is defined and grafana_server.protocol == 'socket'
71-
- grafana_server.socket | dirname != '/var/run'
49+
- grafana_state != 'stopped'
50+
- grafana_server.protocol is undefined or grafana_server.protocol in ['http', 'https']
51+
tags:
52+
- grafana_install
53+
- grafana_configure
54+
- grafana_datasources
55+
- grafana_notifications
56+
- grafana_dashboards
57+
- grafana_run
7258

73-
- name: Enable grafana to ports lower than port 1024
74-
capabilities:
75-
path: /usr/sbin/grafana-server
76-
capability: CAP_NET_BIND_SERVICE+ep
77-
state: present
59+
- name: Wait for grafana to start (socket)
60+
wait_for:
61+
path: "{{ grafana_server.socket }}"
7862
when:
79-
- grafana_port | int <= 1024
80-
- grafana_cap_net_bind_service
63+
- grafana_state != 'stopped'
64+
- grafana_server.protocol is defined and grafana_server.protocol == 'socket'
65+
tags:
66+
- grafana_install
67+
- grafana_configure
68+
- grafana_datasources
69+
- grafana_notifications
70+
- grafana_dashboards
71+
- grafana_run
72+
73+
- include: api_keys.yml
74+
when: grafana_api_keys | length > 0
75+
tags:
76+
- grafana_configure
77+
- grafana_run
78+
79+
- include: datasources.yml
80+
when: grafana_datasources != []
81+
tags:
82+
- grafana_configure
83+
- grafana_datasources
84+
- grafana_run
85+
86+
- include: notifications.yml
87+
when: grafana_alert_notifications | length > 0
88+
tags:
89+
- grafana_configure
90+
- grafana_notifications
91+
- grafana_run
92+
93+
- name: "Check if there are any dashboards in local {{ grafana_dashboards_dir }}"
94+
become: false
95+
set_fact:
96+
found_dashboards: "{{ lookup('fileglob', grafana_dashboards_dir + '/*.json', wantlist=True) }}"
97+
tags:
98+
- grafana_configure
99+
- grafana_dashboards
100+
- grafana_run
81101

82-
- name: Enable and start Grafana systemd unit
83-
systemd:
84-
name: grafana-server
85-
enabled: "{{ grafana_enabled }}"
86-
state: "{{ 'stopped' if grafana_state == 'stopped' else 'started' }}"
87-
daemon_reload: true
102+
- include: dashboards.yml
103+
when: grafana_dashboards | length > 0 or found_dashboards | length > 0
104+
tags:
105+
- grafana_configure
106+
- grafana_dashboards
107+
- grafana_run

tasks/core_configure.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
- name: Ensure grafana configuration directories exist
3+
file:
4+
path: "{{ item }}"
5+
state: directory
6+
owner: root
7+
group: grafana
8+
with_items:
9+
- "/etc/grafana"
10+
- "/etc/grafana/datasources"
11+
- "/etc/grafana/provisioning"
12+
- "/etc/grafana/provisioning/datasources"
13+
- "/etc/grafana/provisioning/dashboards"
14+
- "/etc/grafana/provisioning/notifiers"
15+
- "/etc/grafana/provisioning/plugins"
16+
17+
- name: Create grafana main configuration file
18+
template:
19+
src: grafana.ini.j2
20+
dest: /etc/grafana/grafana.ini
21+
owner: root
22+
group: grafana
23+
mode: 0640
24+
no_log: true
25+
notify: restart grafana
26+
27+
- name: Create grafana LDAP configuration file
28+
template:
29+
src: ldap.toml.j2
30+
dest: "{{ grafana_auth.ldap.config_file | default('/etc/grafana/ldap.toml') }}"
31+
owner: root
32+
group: grafana
33+
mode: 0640
34+
when:
35+
- "'ldap' in grafana_auth"
36+
- "'enabled' not in grafana_auth.ldap or grafana_auth.ldap.enabled"
37+
no_log: true
38+
notify: restart grafana
39+
40+
- name: Create grafana directories
41+
file:
42+
path: "{{ item }}"
43+
state: directory
44+
mode: 0755
45+
owner: "grafana"
46+
group: "grafana"
47+
with_items:
48+
- "{{ grafana_logs_dir }}"
49+
- "{{ grafana_data_dir }}"
50+
- "{{ grafana_data_dir }}/dashboards"
51+
- "{{ grafana_data_dir }}/plugins"
52+
53+
- block:
54+
- name: Create grafana socket directory
55+
file:
56+
path: "{{ grafana_server.socket | dirname }}"
57+
state: directory
58+
mode: 0775
59+
owner: "grafana"
60+
group: "grafana"
61+
62+
- name: Ensure grafana socket directory created on startup
63+
template:
64+
src: tmpfiles.j2
65+
dest: /etc/tmpfiles.d/grafana.conf
66+
owner: root
67+
group: root
68+
mode: 0644
69+
when:
70+
- grafana_server.protocol is defined and grafana_server.protocol == 'socket'
71+
- grafana_server.socket | dirname != '/var/run'
72+
73+
- name: Enable grafana to ports lower than port 1024
74+
capabilities:
75+
path: /usr/sbin/grafana-server
76+
capability: CAP_NET_BIND_SERVICE+ep
77+
state: present
78+
when:
79+
- grafana_port | int <= 1024
80+
- grafana_cap_net_bind_service
81+
82+
- name: Enable and start Grafana systemd unit
83+
systemd:
84+
name: grafana-server
85+
enabled: "{{ grafana_enabled }}"
86+
state: "{{ 'stopped' if grafana_state == 'stopped' else 'started' }}"
87+
daemon_reload: true

tasks/main.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
tags:
2828
- grafana_install
2929

30-
- include: runtime.yml
30+
- include: configure.yml
3131
become: true
3232
tags:
33-
- grafana_runtime
33+
- grafana_configure
34+
- grafana_datasources
35+
- grafana_notifications
36+
- grafana_dashboards

tasks/runtime.yml

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)