1
0
Fork 0
mirror of https://gitlab.com/s3lph/ansible-collection-prometheus synced 2024-11-22 09:41:01 +01:00
ansible-collection-prometheus/roles/prometheus/tasks/prometheus.yml
2020-11-13 10:36:46 +01:00

137 lines
4.5 KiB
YAML

---
- name: create config fragment directory
delegate_to: localhost
run_once: yes
file:
path: "{{ playbook_dir }}/.prometheus/{{ item }}.d"
state: directory
loop:
- conf
- alerts
- name: list current config fragments
delegate_to: localhost
find:
paths:
- "{{ playbook_dir }}/.prometheus/conf.d/"
- "{{ playbook_dir }}/.prometheus/alerts.d/"
recurse: yes
patterns: "*.yml"
excludes: "00-base.yml"
register: prometheus_register_current_fragments
- name: process current config fragments
set_fact:
prometheus_register_current_fragments:
"{{ prometheus_register_current_fragments.files | map(attribute='path') | list }}"
- name: render prometheus base config
delegate_to: localhost
run_once: yes
template:
src: prometheus-base.yml
dest: "{{ playbook_dir }}/.prometheus/conf.d/00-base.yml"
- name: render prometheus job configs
delegate_to: localhost
run_once: yes
template:
src: prometheus-job.yml
dest: "{{ playbook_dir }}/.prometheus/conf.d/{{ '%02d' | format(counter+1) }}-job-{{ item.name }}.yml"
vars:
job: "{{ item }}"
loop: "{{ hostvars[inventory_hostname] | dict2items | selectattr('key', 'match', '^prometheus_job_.+$') | map(attribute='value') | list }}"
loop_control:
index_var: counter
register: prometheus_register_new_config_fragments
- name: remove newly created files from deletion list
set_fact:
prometheus_register_current_fragments:
"{{ prometheus_register_current_fragments | difference(prometheus_register_new_config_fragments.results | map(attribute='dest') | list) }}"
- name: render prometheus alert base config
delegate_to: localhost
run_once: yes
template:
src: prometheus-alert-base.yml
dest: .prometheus/alerts.d/00-base.yml
- name: render prometheus alert configs
delegate_to: localhost
run_once: yes
copy:
content: "{{ item.alerts | to_nice_yaml(indent=2) | indent(2, first=true) }}"
dest: "{{ playbook_dir }}/.prometheus/alerts.d/{{ '%02d' | format(counter+1) }}-alert-{{ item.name }}.yml"
loop: "{{ hostvars[inventory_hostname] | dict2items | selectattr('key', 'match', '^prometheus_job_.+$') | map(attribute='value') | list }}"
loop_control:
index_var: counter
register: prometheus_register_new_alert_fragments
- name: remove newly created files from deletion list
set_fact:
prometheus_register_current_fragments:
"{{ prometheus_register_current_fragments | difference(prometheus_register_new_alert_fragments.results | map(attribute='dest') | list) }}"
- name: render host-specific prometheus alert configs
delegate_to: localhost
copy:
content: "{{ hostvars[item].prometheus_host_specific_alerts | to_nice_yaml(indent=2) | indent(2, first=true) }}"
dest: "{{ playbook_dir }}/.prometheus/alerts.d/{{ '99-host-%s' | format(hostvars[item].inventory_hostname) }}-alerts.yml"
when: "'prometheus_host_specific_alerts' in hostvars[item]"
loop: "{{ hostvars.keys() | list }}"
register: prometheus_register_new_host_specific_alert_fragments
- name: remove newly created files from deletion list
set_fact:
prometheus_register_current_fragments:
"{{ prometheus_register_current_fragments | difference(prometheus_register_new_host_specific_alert_fragments.results | selectattr('dest', 'defined') | map(attribute='dest') | list) }}" # noqa 204
- name: delete old config fragments
delegate_to: localhost
file:
path: "{{ item }}"
state: absent
loop: "{{ prometheus_register_current_fragments }}"
- name: assemble fragment directories
delegate_to: localhost
assemble:
src: "{{ playbook_dir }}/.prometheus/{{ item.local }}.d/"
dest: "{{ playbook_dir }}/.prometheus/{{ item.remote }}.yml"
delimiter: "\n\n"
loop:
- local: conf
remote: prometheus
- local: alerts
remote: alert_rules
- name: upload config files to host
copy:
src: "{{ playbook_dir }}/.prometheus/prometheus.yml"
dest: "/etc/prometheus/prometheus.yml"
owner: root
group: root
mode: 0644
validate: /usr/bin/promtool check config %s
notify: restart prometheus
- name: upload alert config file to host
copy:
src: "{{ playbook_dir }}/.prometheus/alert_rules.yml"
dest: "/etc/prometheus/alert_rules.yml"
owner: root
group: root
mode: 0644
validate: /usr/bin/promtool check rules %s
notify: restart prometheus
- name: configure prometheus lookback delta
lineinfile:
path: /etc/default/prometheus
regexp: "^ARGS=.*$"
line: >-
ARGS="--query.lookback-delta={{ prometheus_lookback_delta }}"
insertbefore: BOF
notify: restart prometheus