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

- name: Make Prometheus postgres-exporter connect to databases using peercred
  lineinfile:
    path: /etc/default/prometheus-postgres-exporter
    regexp: '^DATA_SOURCE_NAME='
    line: |
      DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable"
  notify: Restart prometheus-postgres-exporter

- name: Make Prometheus postgres-exporter launched by postgres user
  lineinfile:
    path: /lib/systemd/system/prometheus-postgres-exporter.service
    regexp: '^User='
    line: |
      User=postgres
  notify: Restart prometheus-postgres-exporter

- name: Make Prometheus postgres-exporter listen on adm only
  lineinfile:
    path: /etc/default/prometheus-postgres-exporter
    regexp: '^ARGS='
    line: |
      ARGS="--web.listen-address={{ ansible_hostname }}.adm.auro.re:9187"
  notify: Restart prometheus-postgres-exporter

- name: Activate prometheus-postgres-exporter service
  systemd:
    name: prometheus-postgres-exporter
    enabled: true
    daemon_reload: true
    state: started