blob: 9ea99ee2a59925b14b4391abe869a5866a27f7c7 (
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
Ansible Role: Prometheus Target
===============================
An Ansible role for deploying Prometheus targets right from your playbooks.
Installation
------------
via galaxy:
```sh
ansible-galaxy collection install kliwniloc.prometheus_target
```
```yaml
# requirements.yml
- src: kliwniloc.prometheus_target
```
via git:
```sh
ansible-galaxy collection install git+https://github.com/kliwniloc/ansible-role-prometheus-target.git,master
```
```yaml
# requirements.yml
- src: https://github.com/kliwniloc/ansible-role-prometheus-target
name: kliwniloc.prometheus_target
```
Role Variables
--------------
For more details see [`defaults/main.yml`](defaults/main.yml)
You need to specify your Prometheus server. The server needs to be present under
that that name in your inventory.
```yaml
prometheus_target_host: "" # Required
```
You should configure defaults for the exporters you are commonly using.
You can configure this in the `prometheus_target_exporter_defaults` variable.
For example if you're using a single target file for node exporters you may
add the path to that file as a default for the node exporter.
This helps keep the `prometheus_target_exporter` variable clean.
Anything configured in the `prometheus_target_exporter` takes precedence over
the defaults.
```yaml
prometheus_target_exporter_defaults: {}
# node_exporter:
# path: /opt/prometheus/targets.yml
# host: '{{ inventory_hostname }}:9100'
# blackbox_exporter:
# path: /opt/prometheus/blackbox.yml
# host: 'https://{{ hostvars[inventory_hostname].ansible_host }}'
prometheus_target_exporter: []
```
You can also add exporter that you want to have deployed without needing to
specify them in the `prometheus_target_exporter` variable by adding them to the
`prometheus_target_default_exporters` variable.
```yaml
prometheus_target_default_exporters: []
prometheus_target_skip_default_exporters: false
```
This role offers a few strategies that you can use to deploy your targets.
The strategy decides how exactly targets are added to the targets file and more
importantly how to handle existing configuration.
* `lineinfile` is the default strategy and simply *appends* a line to the target
file if it isn't already there.
* `yaml` *parses* the yaml target file and adds the host to it. This might mess
with the readability of your yaml file, and you should avoid it if you edit
the yaml file manually as well.
* `json` *parses* the json target file and adds the host to it.
```yaml
prometheus_target_strategy: lineinfile
```
`lineinfile` simply uses the Ansible
[lineinfile](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html)
module to append a line to the specified targets file. This is usually enough to
deploy targets and does not change any of the existing lines of the
configuration which is nice if you comment your target files.
You can configure a prefix and suffix for applying the target to your target
file. The defaults are configured to add a list with 2 (2 space) indentation
levels.
```yaml
prometheus_target_strategy_lineinfile_prefix: ' - '
prometheus_target_strategy_lineinfile_suffix: ''
```
So the target file should look something like this:
```diff
- labels:
my: label
targets:
+ - host:9100
```
There are a few handlers that are notified if a new target is added. You will
want to use those to reload your Prometheus instance after adding modifying
targets. If you manage your target files in git you may also wish to commit the
changes via a hook.
You can enable or disable the handlers via the `prometheus_target_handler_command_enabled`/
`prometheus_target_handler_shell_enabled` variables and configure become
behavior via `prometheus_target_handler_command_become*`/
`prometheus_target_handler_shell_become*`.
The `prometheus_target_handler_command` and `prometheus_target_handler_shell`
variables map the options of their respective Ansible
[command](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html)
and
[shell](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html)
module.
```yaml
prometheus_target_handler_command_enabled: false
prometheus_target_handler_command_become: true
# prometheus_target_handler_command_become_method:
# prometheus_target_handler_command_become_user:
prometheus_target_handler_command: {}
prometheus_target_handler_shell_enabled: false
prometheus_target_handler_shell_become: true
# prometheus_target_handler_shell_become_method:
# prometheus_target_handler_shell_become_user:
prometheus_target_handler_shell: {}
```
Example Playbooks
-----------------
Simple example
```yaml
- name: Deploy node exporter
hosts: myhost
vars: # General configuration. Can be set in group_vars
prometheus_target_host: prometheus
prometheus_target_exporter_defaults:
node_exporter:
path: /opt/prometheus/targets/node.yml
host: '{{ inventory_hostname }}:9100'
roles:
- role: prometheus.node_exporter # deploy node_exporter service
- role: kliwniloc.prometheus_target # deploy target
prometheus_target_exporter:
- id: node_exporter
```
Using Handlers
```yaml
- name: Deploy node exporter and reload Prometheus Docker container
hosts: myhost
vars: # General configuration. Can be set in group_vars
prometheus_target_host: prometheus
prometheus_target_exporter_defaults:
node_exporter:
path: /opt/prometheus/targets/node.yml
host: '{{ inventory_hostname }}:9100'
prometheus_target_handler_command_enabled: true
prometheus_target_handler_command_cmd: docker kill -s SIGHUP prometheus
roles:
- role: prometheus.node_exporter # deploy node_exporter service
- role: kliwniloc.prometheus_target # deploy target
prometheus_target_exporter: [{ id: node_exporter }]
```
Multiple exporters
```yaml
- name: Deploy monitoring
hosts: mycluster
vars: # General configuration. Can be set in group_vars
prometheus_target_host: prometheus
prometheus_target_exporter_defaults:
node_exporter:
path: /opt/prometheus/targets/node.yml
host: '{{ inventory_hostname }}:9100'
blackbox_exporter:
path: /opt/prometheus/targets/blackbox.yml
host: 'https://{{ hostvars[inventory_hostname].ansible_host }}'
roles:
- role: prometheus.node_exporter # deploy node_exporter service
- role: some_application # deploy web app for blackbox exporter
- role: kliwniloc.prometheus_target # deploy targets
prometheus_target_exporter:
- id: node_exporter # deploy node_exporter with default host
# deploy blackbox_exporter with multiple hosts
- { id: blackbox_exporter, host: node1.example.org }
- { id: blackbox_exporter, host: node2.example.org }
- { id: blackbox_exporter, host: node3.example.org }
```
Dependencies
------------
None.
License
-------
MIT
|