aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tasks/strategy_yaml.yml
diff options
context:
space:
mode:
authorMagnus Kühne <73171182+magkue@users.noreply.github.com>2026-02-09 17:23:57 +0100
committerGitHub <noreply@github.com>2026-02-09 17:23:57 +0100
commit049689c124a274870b5405ff6816beeba7d94a14 (patch)
tree583b94ca62c1268bf359d47309ad24823ba9d458 /tasks/strategy_yaml.yml
parent61a0abc5ac4b5d600ee17fef52d7340a9475ac9b (diff)
downloadansible-role-prometheus-target-049689c124a274870b5405ff6816beeba7d94a14.tar.gz
ansible-role-prometheus-target-049689c124a274870b5405ff6816beeba7d94a14.zip
Add support for YAML strategy
Introduces a new 'yaml' strategy using Ansible's `from_yaml` to read target files. This allows users to group multiple targets with distinct label sets (e.g., severity or job) within a single file, optimizing Prometheus file-based service discovery. - Refactored task structure into `strategy_*.yml` for consistency. - Extracted shared exporter variable computation. - Implemented label merging where exporter labels override defaults. - Included support for groups and hosts without labels to prevent crashes. PR: (#1) https://github.com/kliwniloc/ansible-role-prometheus-target/pull/1
Diffstat (limited to 'tasks/strategy_yaml.yml')
-rw-r--r--tasks/strategy_yaml.yml71
1 files changed, 71 insertions, 0 deletions
diff --git a/tasks/strategy_yaml.yml b/tasks/strategy_yaml.yml
new file mode 100644
index 0000000..1d6df77
--- /dev/null
+++ b/tasks/strategy_yaml.yml
@@ -0,0 +1,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 }}'