Skip to content

Commit 4b437aa

Browse files
committed
fix bug where prometheus environments didn't work
1 parent e9ffefd commit 4b437aa

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

ansible/filter_plugins/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
import os.path
1212
import re
1313

14-
def prometheus_node_exporter_targets(hosts, env):
14+
def prometheus_node_exporter_targets(hosts, hostvars, env_key):
1515
result = []
1616
per_env = defaultdict(list)
1717
for host in hosts:
18-
per_env[env].append(host)
18+
host_env = hostvars[host].get(env_key, 'ungrouped')
19+
per_env[host_env].append(host)
1920
for env, hosts in per_env.items():
2021
target = {
2122
"targets": ["{target}:9100".format(target=target) for target in hosts],

environments/common/inventory/group_vars/all/prometheus.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ prometheus_alert_rules_files: "{{ ansible_inventory_sources | product([prometheu
2222

2323
prometheus_alert_rules: []
2424

25-
# Can set a hostvar 'env' to an arbitrary string to group prometheus targets, e.g. rack.
26-
# env: location-1
25+
# Can set a hostvar 'prometheus_env' to an arbitrary string to group prometheus targets, e.g. by rack.
2726
prometheus_targets:
28-
node: "{{ groups.get('node_exporter', []) | reject('equalto', 'localhost') | prometheus_node_exporter_targets(env | default('ungrouped')) }}"
27+
node: "{{ groups.get('node_exporter', []) | prometheus_node_exporter_targets(hostvars, 'prometheus_env') }}"
2928

3029
prometheus_scrape_configs_default:
3130
- job_name: "prometheus"
@@ -40,7 +39,7 @@ prometheus_scrape_configs_default:
4039
- job_name: "node"
4140
file_sd_configs:
4241
- files:
43-
- "/etc/prometheus/file_sd/node.yml"
42+
- /etc/prometheus/file_sd/node.yml
4443
relabel_configs:
4544
# strip off port
4645
- source_labels: ['__address__']

0 commit comments

Comments
 (0)