---
- name: Install Prometheus
  apt:
    update_cache: true
    name:
      - prometheus
  register: apt_result
  retries: 3
  until: apt_result is succeeded

- name: Configure Prometheus
  template:
    src: "{{ item }}.j2"
    dest: "/etc/prometheus/{{ item }}"
    owner: prometheus
    group: prometheus
    mode: u=r,g=r,o=
  loop:
    - prometheus.yml
    - alert.rules.yml
  notify: Restart Prometheus

- name: Define Prometheus retention time
  copy:
    src: files/prometheus
    dest: /etc/default/prometheus
    owner: prometheus
    group: prometheus
    mode: u=r,g=r,o=
  notify: Restart Prometheus

# We don't need to restart Prometheus when updating nodes
- name: Configure Prometheus Federate devices
  copy:
    content: "{{ [{'targets': prometheus_targets }] | to_nice_json }}"
    dest: /etc/prometheus/targets.json
    mode: 0644
  when: prometheus_targets is defined

- name: Activate prometheus service
  systemd:
    name: prometheus
    enabled: true
    state: started
...