Skip to content

Commit 5ca7dd7

Browse files
refactor: ansible
1 parent 531b835 commit 5ca7dd7

10 files changed

Lines changed: 312 additions & 64 deletions

File tree

.gitignore

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
**/hosts
55
**/node-exporter.json
66
**/prometheus.yml
7+
data/*
8+
facts/*
79

810
# General
911
.DS_Store
@@ -226,6 +228,137 @@ cython_debug/
226228
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
227229
#.idea/
228230

231+
# Logs
232+
logs
233+
*.log
234+
npm-debug.log*
235+
yarn-debug.log*
236+
yarn-error.log*
237+
lerna-debug.log*
238+
.pnpm-debug.log*
239+
240+
# Diagnostic reports (https://nodejs.org/api/report.html)
241+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
242+
243+
# Runtime data
244+
pids
245+
*.pid
246+
*.seed
247+
*.pid.lock
248+
249+
# Directory for instrumented libs generated by jscoverage/JSCover
250+
lib-cov
251+
252+
# Coverage directory used by tools like istanbul
253+
coverage
254+
*.lcov
255+
256+
# nyc test coverage
257+
.nyc_output
258+
259+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
260+
.grunt
261+
262+
# Bower dependency directory (https://bower.io/)
263+
bower_components
264+
265+
# node-waf configuration
266+
.lock-wscript
267+
268+
# Compiled binary addons (https://nodejs.org/api/addons.html)
269+
build/Release
270+
271+
# Dependency directories
272+
node_modules/
273+
jspm_packages/
274+
275+
# Snowpack dependency directory (https://snowpack.dev/)
276+
web_modules/
277+
278+
# TypeScript cache
279+
*.tsbuildinfo
280+
281+
# Optional npm cache directory
282+
.npm
283+
284+
# Optional eslint cache
285+
.eslintcache
286+
287+
# Optional stylelint cache
288+
.stylelintcache
289+
290+
# Microbundle cache
291+
.rpt2_cache/
292+
.rts2_cache_cjs/
293+
.rts2_cache_es/
294+
.rts2_cache_umd/
295+
296+
# Optional REPL history
297+
.node_repl_history
298+
299+
# Output of 'npm pack'
300+
*.tgz
301+
302+
# Yarn Integrity file
303+
.yarn-integrity
304+
305+
# dotenv environment variable files
306+
.env
307+
.env.development.local
308+
.env.test.local
309+
.env.production.local
310+
.env.local
311+
312+
# parcel-bundler cache (https://parceljs.org/)
313+
.cache
314+
.parcel-cache
315+
316+
# Next.js build output
317+
.next
318+
out
319+
320+
# Nuxt.js build / generate output
321+
.nuxt
322+
dist
323+
324+
# Gatsby files
325+
.cache/
326+
# Comment in the public line in if your project uses Gatsby and not Next.js
327+
# https://nextjs.org/blog/next-9-1#public-directory-support
328+
# public
329+
330+
# vuepress build output
331+
.vuepress/dist
332+
333+
# vuepress v2.x temp and cache directory
334+
.temp
335+
.cache
336+
337+
# Docusaurus cache and generated files
338+
.docusaurus
339+
340+
# Serverless directories
341+
.serverless/
342+
343+
# FuseBox cache
344+
.fusebox/
345+
346+
# DynamoDB Local files
347+
.dynamodb/
348+
349+
# TernJS port file
350+
.tern-port
351+
352+
# Stores VSCode versions used for testing VSCode extensions
353+
.vscode-test
354+
355+
# yarn v2
356+
.yarn/cache
357+
.yarn/unplugged
358+
.yarn/build-state.yml
359+
.yarn/install-state.gz
360+
.pnp.*
361+
229362
# INCLUDE
230363
!**/*.example
231364
!**/.gitkeep

ansible.cfg

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[defaults]
2+
log_path = /var/log/ansible.log
3+
inventory = ./hosts
4+
roles_path = ./roles
5+
host_key_checking = False
6+
retry_files_enabled = False
7+
remote_tmp = /tmp/${USER}/ansible
8+
gathering = smart
9+
fact_caching = jsonfile
10+
fact_caching_connection = ./facts
11+
fact_caching_prefix = ansible_facts_

config/prometheus.yml.tpl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ scrape_configs:
99
- job_name: 'node'
1010
static_configs:
1111
- targets: [
12-
'node-exporter:${node_exporter_port}', # Local container using Docker network DNS
13-
%{ for host in node_exporter_hosts ~}
14-
'${host.ip}:${node_exporter_port}', # Remote hosts using IP addresses
15-
%{ endfor ~}
12+
'node-exporter:${node_exporter_port}',
13+
%{~ for host in node_exporter_hosts ~}
14+
'${host.ip}:${node_exporter_port}',
15+
%{~ endfor ~}
1616
]

exporter.yml

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,104 @@
11
---
2-
- name: Deploy exporters as containers
2+
- name: Deploy monitoring exporters
33
hosts: all
4-
gather_facts: false
5-
vars:
6-
exporter_defaults:
7-
node:
8-
image: "prom/node-exporter:latest"
9-
port: 9100
10-
mongodb:
11-
image: "bitnami/mongodb-exporter:latest"
12-
port: 9216
13-
redis:
14-
image: "oliver006/redis_exporter:latest"
15-
port: 9121
16-
4+
gather_facts: true
5+
environment:
6+
PATH: "{{ ansible_env.HOME }}/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/sbin:/bin:{{ ansible_env.PATH }}"
7+
vars_files:
8+
- vars.yml
179
tasks:
18-
- name: Set exporter type based on inventory group
19-
ansible.builtin.set_fact:
20-
exporter_type: >-
21-
{%- if inventory_hostname in groups.get('node_exporters', []) -%}
22-
node
23-
{%- elif inventory_hostname in groups.get('mongodb_hosts', []) -%}
24-
mongodb
25-
{%- elif inventory_hostname in groups.get('redis_hosts', []) -%}
26-
redis
27-
{%- endif -%}
10+
- name: Create monitoring network
11+
community.docker.docker_network:
12+
name: "{{ item.name }}"
13+
state: present
14+
driver: "{{ item.driver }}"
15+
loop: "{{ docker_networks }}"
16+
delegate_to: "{{ groups['all'][0] }}"
17+
18+
- name: Pull exporter images
19+
community.docker.docker_container:
20+
name: "{{ item.image }}"
21+
pull: missing
22+
loop: "{{ exporters }}"
23+
24+
- name: Deploy node-exporter
25+
community.docker.docker_container:
26+
name: "node-exporter"
27+
image: "{{ exporters.node.image }}"
28+
state: started
29+
restart_policy: unless-stopped
30+
networks:
31+
- name: "{{ docker_networks[0].name }}"
32+
published_ports:
33+
- "{{ exporters.node.port }}:{{ exporters.node.port }}"
34+
volumes:
35+
- "/proc:/host/proc:ro"
36+
- "/sys:/host/sys:ro"
37+
- "/:/rootfs:ro"
38+
command:
39+
- "--path.procfs=/host/proc"
40+
- "--path.sysfs=/host/sys"
41+
- "--path.rootfs=/rootfs"
42+
- "--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)"
43+
when: "'node' in exporters"
44+
45+
- name: Deploy mongodb exporter
46+
community.docker.docker_container:
47+
name: "mongodb-exporter-{{ item.name }}"
48+
image: "{{ exporters.mongodb.image }}"
49+
state: started
50+
restart_policy: unless-stopped
51+
networks:
52+
- name: "{{ docker_networks[0].name }}"
53+
published_ports:
54+
- "{{ exporters.mongodb.port }}:{{ exporters.mongodb.port }}"
55+
command: "--mongodb.uri=mongodb://{{ item.ip }}:{{ item.port }}/"
56+
when: "'mongodb' in active_exporters"
57+
loop: "{{ service_hosts | selectattr('service', 'equalto', 'mongodb') | list }}"
58+
59+
- name: Deploy redis exporter
60+
community.docker.docker_container:
61+
name: "redis-exporter-{{ item.name }}"
62+
image: "{{ exporters.redis.image }}"
63+
state: started
64+
restart_policy: unless-stopped
65+
networks:
66+
- name: "{{ docker_networks[0].name }}"
67+
published_ports:
68+
- "{{ exporters.redis.port }}:{{ exporters.redis.port }}"
69+
command: "--redis.addr=redis://{{ item.ip }}:{{ item.port }}"
70+
when: "'redis' in active_exporters"
71+
loop: "{{ service_hosts | selectattr('service', 'equalto', 'redis') | list }}"
72+
73+
- name: Deploy elasticsearch exporter
74+
community.docker.docker_container:
75+
name: "elasticsearch-exporter-{{ item.name }}"
76+
image: "{{ exporters.elasticsearch.image }}"
77+
state: started
78+
restart_policy: unless-stopped
79+
networks:
80+
- name: "{{ docker_networks[0].name }}"
81+
published_ports:
82+
- "{{ exporters.elasticsearch.port }}:{{ exporters.elasticsearch.port }}"
83+
command: >-
84+
--es.uri=http://{{ item.ip }}:{{ item.port }}
85+
--es.all
86+
--es.indices
87+
--es.indices_settings
88+
--es.cluster_settings
89+
when: "'elasticsearch' in active_exporters"
90+
loop: "{{ service_hosts | selectattr('service', 'equalto', 'elasticsearch') | list }}"
2891

29-
- name: Start exporter container
92+
- name: Deploy kafka exporter
3093
community.docker.docker_container:
31-
name: "{{ exporter_type }}-exporter"
32-
image: "{{ exporter_defaults[exporter_type].image }}"
94+
name: "kafka-exporter-{{ item.name }}"
95+
image: "{{ exporters.kafka.image }}"
3396
state: started
3497
restart_policy: unless-stopped
98+
networks:
99+
- name: "{{ docker_networks[0].name }}"
35100
published_ports:
36-
- "{{ exporter_defaults[exporter_type].port }}:{{ exporter_defaults[exporter_type].port }}"
37-
container_default_behavior: no_defaults
38-
command: "{{ container_command | default(omit) }}"
39-
vars:
40-
container_command: >-
41-
{%- if exporter_type == 'mongodb' -%}
42-
--mongodb.uri=mongodb://localhost:{{ service_port }}/
43-
{%- elif exporter_type == 'redis' -%}
44-
--redis.addr=redis://localhost:{{ service_port }}/
45-
{%- endif -%}
46-
when: exporter_type is defined and exporter_type != ''
101+
- "{{ exporters.kafka.port }}:{{ exporters.kafka.port }}"
102+
command: "--kafka.server={{ item.ip }}:{{ item.port }}"
103+
when: "'kafka' in active_exporters"
104+
loop: "{{ service_hosts | selectattr('service', 'equalto', 'kafka') | list }}"

facts/.gitkeep

Whitespace-only changes.

main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ resource "null_resource" "ansible_playbook" {
176176

177177
triggers = {
178178
inventory_content = local_file.ansible_inventory.content
179-
playbook_hash = filemd5("${path.module}/node_exporter.yml")
179+
playbook_hash = filemd5("${path.module}/${var.playbook}")
180180
}
181181

182182
provisioner "local-exec" {
183-
command = "ansible-playbook -i ${local_file.ansible_inventory.filename} node_exporter.yml"
183+
command = "ansible-playbook -i ${local_file.ansible_inventory.filename} exporter.yml"
184184
}
185185
}

node_exporter.yml

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

resources.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@ resource "docker_network" "promgraf_network" {
55
resource "docker_volume" "prometheus_data" {
66
name = "prometheus-data"
77
driver = "local"
8+
driver_opts = {
9+
type = "none"
10+
device = "${abspath(path.cwd)}/data/prometheus"
11+
o = "bind"
12+
}
13+
14+
lifecycle {
15+
prevent_destroy = false
16+
}
817
}
918

1019
resource "docker_volume" "grafana_data" {
1120
name = "grafana-data"
1221
driver = "local"
22+
driver_opts = {
23+
type = "none"
24+
device = "${abspath(path.cwd)}/data/grafana"
25+
o = "bind"
26+
}
27+
28+
lifecycle {
29+
prevent_destroy = false
30+
}
1331
}
1432

1533
resource "docker_image" "prometheus" {

0 commit comments

Comments
 (0)