---
- name: alerting rules file
  template:
    src: "alert.rules.j2"
    dest: "{{ prometheus_config_dir }}/rules/ansible_managed.rules"
    owner: root
    group: prometheus
    mode: 0640
    validate: "{{ prometheus_binary_install_dir }}/promtool check rules %s"
  when:
    - prometheus_alert_rules != []
  notify:
    - reload prometheus

- name: copy custom alerting rule files
  copy:
    src: "{{ item }}"
    dest: "{{ prometheus_config_dir }}/rules/"
    owner: root
    group: prometheus
    mode: 0640
    validate: "{{ prometheus_binary_install_dir }}/promtool check rules %s"
  with_fileglob: "{{ prometheus_alert_rules_files }}"
  notify:
    - reload prometheus

- name: configure prometheus
  template:
    src: "{{ prometheus_config_file }}"
    dest: "{{ prometheus_config_dir }}/prometheus.yml"
    force: true
    owner: root
    group: prometheus
    mode: 0640
    validate: "{{ prometheus_binary_install_dir }}/promtool check config %s"
  notify:
    - reload prometheus

- name: configure Prometheus web
  copy:
    content: "{{ prometheus_web_config | to_nice_yaml(indent=2,sort_keys=False) }}"
    dest: "{{ prometheus_config_dir }}/web.yml"
    force: true
    owner: root
    group: prometheus
    mode: 0640

- name: configure prometheus static targets
  copy:
    content: |
      #jinja2: lstrip_blocks: True
      {{ item.value | to_nice_yaml(indent=2,sort_keys=False) }}
    dest: "{{ prometheus_config_dir }}/file_sd/{{ item.key }}.yml"
    force: true
    owner: root
    group: prometheus
    mode: 0640
  with_dict: "{{ prometheus_targets }}"
  when: prometheus_targets != {}

- name: copy prometheus custom static targets
  copy:
    src: "{{ item }}"
    dest: "{{ prometheus_config_dir }}/file_sd/"
    force: true
    owner: root
    group: prometheus
    mode: 0640
  with_fileglob: "{{ prometheus_static_targets_files }}"
