---
- name: Install Prometheus
  apt:
    update_cache: true
    name:
      - prometheus
      - prometheus-snmp-exporter
  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
  notify: Restart Prometheus

- name: Creates directory for alerts
  file:
    path: /etc/prometheus/alerts
    state: directory
    owner: prometheus
    group: prometheus
    mode: 0755

- name: Configure Prometheus alerts
  template:
    src: "{{ item }}.j2"
    dest: "/etc/prometheus/alerts/{{ item }}"
    owner: prometheus
    group: prometheus
    mode: u=r,g=r,o=
  loop:
    - server.rules.yml
    - docker.rules.yml
    - django.rules.yml
    - ups.rules.yml
    - postgres.rules.yml
    - environmental.rules.yml
  notify: Restart Prometheus

- name: Make Prometheus snmp-exporter listen on localhost only
  lineinfile:
    path: /etc/default/prometheus-snmp-exporter
    regexp: '^ARGS='
    line: "ARGS=\"--web.listen-address=127.0.0.1:9116\""
  notify: Restart prometheus-snmp-exporter

# These files store SNMP OIDs
- name: Configure Prometheus snmp-exporter
  template:
    src: "{{ item }}.j2"
    dest: "/etc/prometheus/{{ item }}"
    owner: prometheus
    group: prometheus
    mode: u=r,g=r,o=
  loop:
    - snmp.yml
  notify: Restart prometheus-snmp-exporter

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

# We don't need to restart Prometheus when updating nodes
- name: Configure Prometheus Ubiquity Unifi SNMP devices
  copy:
    content: "{{ prometheus_unifi_snmp_targets | to_nice_json }}"
    dest: /etc/prometheus/targets_unifi_snmp.json
    mode: 0644
  when: prometheus_unifi_snmp_targets is defined

- name: Configure Prometheus Switchs
  copy:
    content: "{{ prometheus_switch_snmp_targets | to_nice_json }}"
    dest: /etc/prometheus/targets_switch_snmp.json
    mode: 0644
  when: prometheus_switch_snmp_targets is defined

- name: Configure Prometheus UPS SNMP devices
  copy:
    content: "{{ [{'targets': prometheus_ups_snmp_targets }] | to_nice_json }}\n"
    dest: /etc/prometheus/targets_ups_snmp.json
    mode: 0644
  when: prometheus_ups_snmp_targets is defined

- name: Configure Prometheus docker monitoring
  copy:
    content: "{{ [{'targets': prometheus_docker_targets }] | to_nice_json }}\n"
    dest: /etc/prometheus/targets_docker.json
    mode: 0644
  when: prometheus_docker_targets is defined

- name: Configure Prometheus postgres monitoring
  copy:
    content: "{{ prometheus_postgres_targets | to_nice_json }}\n"
    dest: /etc/prometheus/targets_postgres.json
    mode: 0644
  when: prometheus_postgres_targets is defined

- name: Configure Prometheus apc_pdu monitoring
  copy:
    content: "{{ [{'targets': prometheus_pdu_snmp_targets }] | to_nice_json }}\n"
    dest: /etc/prometheus/targets_apc_pdu_snmp.json
    mode: 0644
  when: prometheus_pdu_snmp_targets is defined

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