1
0
Fork 0
mirror of https://gitlab.com/s3lph/ansible-collection-prometheus synced 2024-10-23 11:56:59 +02:00
ansible-collection-prometheus/roles/prometheus/tasks/prometheus.yml

145 lines
4.9 KiB
YAML
Raw Normal View History

2020-11-13 10:36:46 +01:00
---
- name: create config fragment directory
delegate_to: localhost
run_once: yes
ansible.builtin.file:
2020-11-13 10:36:46 +01:00
path: "{{ playbook_dir }}/.prometheus/{{ item }}.d"
state: directory
mode: 0755
2020-11-13 10:36:46 +01:00
loop:
- conf
- alerts
- name: list current config fragments
delegate_to: localhost
ansible.builtin.find:
2020-11-13 10:36:46 +01:00
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
ansible.builtin.set_fact:
2020-11-13 10:36:46 +01:00
prometheus_register_current_fragments:
"{{ prometheus_register_current_fragments.files | map(attribute='path') | list }}"
- name: render prometheus base config
delegate_to: localhost
run_once: yes
ansible.builtin.template:
2020-11-13 10:36:46 +01:00
src: prometheus-base.yml
dest: "{{ playbook_dir }}/.prometheus/conf.d/00-base.yml"
mode: 0644
2020-11-13 10:36:46 +01:00
- name: render prometheus job configs
delegate_to: localhost
run_once: yes
ansible.builtin.template:
2020-11-13 10:36:46 +01:00
src: prometheus-job.yml
dest: "{{ playbook_dir }}/.prometheus/conf.d/{{ '%02d' | format(counter+1) }}-job-{{ item.name }}.yml"
mode: 0644
2020-11-13 10:36:46 +01:00
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
ansible.builtin.set_fact:
2020-11-13 10:36:46 +01:00
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
ansible.builtin.template:
2020-11-13 10:36:46 +01:00
src: prometheus-alert-base.yml
dest: .prometheus/alerts.d/00-base.yml
mode: 0644
2020-11-13 10:36:46 +01:00
- name: render prometheus alert configs
delegate_to: localhost
run_once: yes
ansible.builtin.copy:
2020-11-13 10:36:46 +01:00
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"
mode: 0644
2020-11-13 10:36:46 +01:00
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
ansible.builtin.set_fact:
2020-11-13 10:36:46 +01:00
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
ansible.builtin.copy:
2020-11-13 10:36:46 +01:00
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"
mode: 0644
2020-11-13 10:36:46 +01:00
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
ansible.builtin.set_fact:
2020-11-13 10:36:46 +01:00
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
ansible.builtin.file:
2020-11-13 10:36:46 +01:00
path: "{{ item }}"
state: absent
loop: "{{ prometheus_register_current_fragments }}"
- name: assemble fragment directories
delegate_to: localhost
ansible.builtin.assemble:
2020-11-13 10:36:46 +01:00
src: "{{ playbook_dir }}/.prometheus/{{ item.local }}.d/"
dest: "{{ playbook_dir }}/.prometheus/{{ item.remote }}.yml"
delimiter: "\n\n"
mode: 0644
2020-11-13 10:36:46 +01:00
loop:
- local: conf
remote: prometheus
- local: alerts
remote: alert_rules
- name: upload config files to host
ansible.builtin.copy:
2020-11-13 10:36:46 +01:00
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
ansible.builtin.copy:
2020-11-13 10:36:46 +01:00
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
ansible.builtin.lineinfile:
2020-11-13 10:36:46 +01:00
path: /etc/default/prometheus
regexp: "^ARGS=.*$"
line: >-
ARGS="--query.lookback-delta={{ prometheus_lookback_delta }}"
insertbefore: BOF
notify: restart prometheus