summaryrefslogtreecommitdiff
path: root/roles/monitoring/grafana/tasks/main.yml
blob: 5d6c9c1739fa340d8fc5eced97479c8418ca4ecd (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
- name: add debian repository
  include_role:
    name: apt-repo/grafana

- name: generate apt pin file for grafana package
  when: grafana_version is defined
  copy:
    dest: "/etc/apt/preferences.d/grafana.pref"
    content: |
      Package: grafana
      Pin: version {{ grafana_version }}
      Pin-Priority: 1001

- name: remove apt pin file for grafana package
  when: grafana_version is not defined
  file:
    path: "/etc/apt/preferences.d/grafana.pref"
    state: absent

- name: install apt packages
  apt:
    name: "grafana{% if grafana_version is defined %}={{ grafana_version }}{% endif %}"
    state: present
    allow_downgrade: yes

- name: configure grafana server
  loop: "{{ grafana_config_server | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  ini_file:
    path: /etc/grafana/grafana.ini
    section: server
    option: "{{ item.key }}"
    value: "{{ item.value | string }}"
  notify: restart grafana

- name: configure grafana analytics
  loop: "{{ grafana_config_analytics | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  ini_file:
    path: /etc/grafana/grafana.ini
    section: analytics
    option: "{{ item.key }}"
    value: "{{ item.value | string }}"
  notify: restart grafana

- name: configure grafana security
  loop: "{{ grafana_config_security | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  ini_file:
    path: /etc/grafana/grafana.ini
    section: security
    option: "{{ item.key }}"
    value: "{{ item.value | string }}"
  notify: restart grafana

- name: configure grafana users
  loop: "{{ grafana_config_users | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  ini_file:
    path: /etc/grafana/grafana.ini
    section: users
    option: "{{ item.key }}"
    value: "{{ item.value | string }}"
  notify: restart grafana

- name: configure grafana auth
  loop: "{{ grafana_config_auth | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  ini_file:
    path: /etc/grafana/grafana.ini
    section: auth
    option: "{{ item.key }}"
    value: "{{ item.value | string }}"
  notify: restart grafana

- name: configure grafana auth.proxy
  loop: "{{ grafana_config_auth_proxy | dict2items }}"
  loop_control:
    label: "{{ item.key }}"
  ini_file:
    path: /etc/grafana/grafana.ini
    section: auth.proxy
    option: "{{ item.key }}"
    value: "{{ item.value | string }}"
  notify: restart grafana


- name: install datasources
  copy:
    dest: "/etc/grafana/provisioning/datasources/ansible.yml"
    content: |
      apiVersion: 1
      deleteDatasources: []
      {{ {'datasources': grafana_datasources} | to_nice_yaml(indent=2) }}
    group: grafana
    mode: 0640
  no_log: yes
  notify: restart grafana

- name: configure dashboard provisioning
  copy:
    dest: "/etc/grafana/provisioning/dashboards/ansible.yml"
    content: |
      apiVersion: 1
      providers:
      - name: ansible'
        folder: ''
        options:
          path: /var/lib/grafana/dashboards/ansible
          foldersFromFilesStructure: true
    group: grafana
    mode: 0640
  notify: restart grafana

- name: create base directory for dashboards
  file:
    path: /var/lib/grafana/dashboards/ansible
    state: directory
    group: grafana
    mode: 0750

- name: create sub-directories for dashboards
  loop: "{{ grafana_dashboards | grafana_dashboard_subdirs }}"
  file:
    path: "/var/lib/grafana/dashboards/ansible/{{ item }}"
    state: directory
    group: grafana
    mode: 0750

- name: install dashboards from file
  loop: "{{ grafana_dashboards | selectattr('file', 'defined') }}"
  loop_control:
    label: "{{ item.file }} -> {{ item.datasource }}"
  copy:
    content: "{{ lookup('file', 'dashboard-'+item.file+'.json') | regex_replace('\"(?:\\${)?DS_[A-Z0-9_-]+(?:})?\"', '\"'+item.datasource+'\"') }}\n"
    dest: "/var/lib/grafana/dashboards/ansible/{{ item | grafana_dashboard_dest }}.json"
  register: grafana_dasboards_from_file

- name: install dashboards from content
  loop: "{{ grafana_dashboards | selectattr('content', 'defined') }}"
  loop_control:
    label: "{{ item.name }} -> {{ item.datasource }}"
  copy:
    content: "{{ item.content | regex_replace('\"(?:\\${)?DS_[A-Z0-9_-]+(?:})?\"', '\"'+item.datasource+'\"') }}\n"
    dest: "/var/lib/grafana/dashboards/ansible/{{ item | grafana_dashboard_dest }}.json"
  register: grafana_dasboards_from_content

- name: fetch list of installed dashboards
  find:
    paths: /var/lib/grafana/dashboards/ansible
    patterns: "*.json"
    recurse: true
  register: grafana_dashboards_installed

- name: remove superfluous dashboards
  loop: "{{ grafana_dashboards_installed.files | map(attribute='path') | difference(grafana_dasboards_from_file.results | map(attribute='dest')) | difference(grafana_dasboards_from_content.results | map(attribute='dest')) | list }}"
  file:
    path: "{{ item }}"
    state: absent

- name: make sure grafana-server is enabled and started
  systemd:
    name: grafana-server
    state: started
    enabled: yes

- name: manage grafana users
  include_tasks: users.yml