blob: 1d6df777382c3e8d09d24fdc65cb6a03680a7cb1 (
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
|
---
- name: Check if target file exists
ansible.builtin.stat:
path: '{{ _target_path }}'
register: _target_file_stat
delegate_to: '{{ prometheus_target_host }}'
- name: Read target file
ansible.builtin.slurp:
src: '{{ _target_path }}'
register: _target_file_content
delegate_to: '{{ prometheus_target_host }}'
when: _target_file_stat.stat.exists
- name: Parse YAML and add host to matching target group
ansible.builtin.set_fact:
_updated_targets: >-
{%- set targets = (_target_file_content.content | b64decode | from_yaml) if _target_file_stat.stat.exists else [] -%}
{%- set targets = targets if targets is not none else [] -%}
{%- set ns = namespace(found=false, result=[], match=true) -%}
{%- for group in targets -%}
{%- set group_labels = group.labels | default({}) -%}
{%- set ns.match = true -%}
{%- if _target_labels | length != group_labels | length -%}
{%- set ns.match = false -%}
{%- else -%}
{%- for key, value in _target_labels.items() -%}
{%- if group_labels.get(key) != value -%}
{%- set ns.match = false -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if ns.match -%}
{%- if _target_host not in group.targets -%}
{%- set _ = group.targets.append(_target_host) -%}
{%- endif -%}
{%- set ns.found = true -%}
{%- set _ = ns.result.append(group) -%}
{%- else -%}
{%- set filtered_targets = group.targets | reject('equalto', _target_host) | list -%}
{%- if filtered_targets | length > 0 -%}
{%- if group_labels | length > 0 -%}
{%- set _ = ns.result.append({'labels': group_labels, 'targets': filtered_targets}) -%}
{%- else -%}
{%- set _ = ns.result.append({'targets': filtered_targets}) -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if not ns.found -%}
{%- if _target_labels | length > 0 -%}
{%- set new_group = {'labels': _target_labels, 'targets': [_target_host]} -%}
{%- else -%}
{%- set new_group = {'targets': [_target_host]} -%}
{%- endif -%}
{%- set _ = ns.result.append(new_group) -%}
{%- endif -%}
{{ ns.result }}
- name: Write updated target file
ansible.builtin.copy:
content: '{{ _updated_targets | to_nice_yaml(indent=2, width=1337) }}'
dest: '{{ _target_path }}'
mode: '0644'
delegate_to: '{{ prometheus_target_host }}'
become: true
register: _yaml_result
- name: Track changes
ansible.builtin.set_fact:
changed: '{{ changed | default(false) or _yaml_result.changed }}'
|