diff options
| author | Magnus Kühne <73171182+magkue@users.noreply.github.com> | 2026-02-09 17:23:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-02-09 17:23:57 +0100 |
| commit | 049689c124a274870b5405ff6816beeba7d94a14 (patch) | |
| tree | 583b94ca62c1268bf359d47309ad24823ba9d458 /tasks | |
| parent | 61a0abc5ac4b5d600ee17fef52d7340a9475ac9b (diff) | |
| download | ansible-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')
| -rw-r--r-- | tasks/lineinfile.yml | 24 | ||||
| -rw-r--r-- | tasks/main.yml | 12 | ||||
| -rw-r--r-- | tasks/process_exporter.yml | 18 | ||||
| -rw-r--r-- | tasks/strategy_lineinfile.yml | 13 | ||||
| -rw-r--r-- | tasks/strategy_yaml.yml | 71 |
5 files changed, 109 insertions, 29 deletions
diff --git a/tasks/lineinfile.yml b/tasks/lineinfile.yml deleted file mode 100644 index 8c60ba0..0000000 --- a/tasks/lineinfile.yml +++ /dev/null @@ -1,24 +0,0 @@ ---- -- name: Make sure targets are deployed - ansible.builtin.lineinfile: - path: '{{ ( - item.path_prefix if item.path_prefix is defined else - (prometheus_target_exporter_defaults[item.id].path_prefix - | default(prometheus_target_exporter_target_prefix)) - if item.id is defined) - ~ - (item.path if item.path is defined - else prometheus_target_exporter_defaults[item.id].path if item.id is defined) | mandatory }}' - line: '{{ prometheus_target_strategy_lineinfile_prefix ~ - (item.host if item.host is defined else prometheus_target_exporter_defaults[item.id].host) | mandatory ~ - prometheus_target_strategy_lineinfile_suffix }}' - state: present - become: true - delegate_to: '{{ prometheus_target_host }}' - loop: '{{ prometheus_target_exporter + - ([] if prometheus_target_skip_default_exporters else prometheus_target_default_exporters) }}' - register: lineinfile - -- name: Export fact - ansible.builtin.set_fact: - changed: '{{ changed or lineinfile.changed }}' diff --git a/tasks/main.yml b/tasks/main.yml index f8d6caa..5a184f8 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,11 +1,13 @@ --- -- name: Select strategy +- name: Process all exporters ansible.builtin.include_tasks: - file: '{{ prometheus_target_strategy }}.yml' - vars: - changed: false + file: process_exporter.yml + loop: '{{ prometheus_target_exporter + + ([] if prometheus_target_skip_default_exporters else prometheus_target_default_exporters) }}' + loop_control: + loop_var: item - name: Run handlers ansible.builtin.include_tasks: file: handlers.yml - when: changed + when: changed | default(false) diff --git a/tasks/process_exporter.yml b/tasks/process_exporter.yml new file mode 100644 index 0000000..2d13b29 --- /dev/null +++ b/tasks/process_exporter.yml @@ -0,0 +1,18 @@ +--- +- name: Set exporter variables + vars: + _defaults: '{{ prometheus_target_exporter_defaults[item.id] | default({}) if item.id is defined else {} }}' + ansible.builtin.set_fact: + _target_path: >- + {{ (item.path_prefix if item.path_prefix is defined else + (_defaults.path_prefix | default(prometheus_target_exporter_target_prefix))) + ~ + (item.path if item.path is defined else _defaults.path) | mandatory }} + _target_host: >- + {{ (item.host if item.host is defined else _defaults.host) | mandatory }} + _target_labels: >- + {{ (_defaults.labels | default({})) | combine(item.labels | default({})) }} + +- name: Execute strategy + ansible.builtin.include_tasks: + file: strategy_{{ prometheus_target_strategy }}.yml diff --git a/tasks/strategy_lineinfile.yml b/tasks/strategy_lineinfile.yml new file mode 100644 index 0000000..a5dd01f --- /dev/null +++ b/tasks/strategy_lineinfile.yml @@ -0,0 +1,13 @@ +--- +- name: Deploy target via lineinfile + ansible.builtin.lineinfile: + path: '{{ _target_path }}' + line: '{{ prometheus_target_strategy_lineinfile_prefix ~ _target_host ~ prometheus_target_strategy_lineinfile_suffix }}' + state: present + delegate_to: '{{ prometheus_target_host }}' + become: true + register: _lineinfile_result + +- name: Track changes + ansible.builtin.set_fact: + changed: '{{ changed | default(false) or _lineinfile_result.changed }}' 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 }}' |