---
- name: Ensure nginx configuration is up to date
  become: yes
  template:
    src: "prometheus_nginx.conf.j2"
    dest: "/etc/nginx/sites-available/prometheus.conf"
  notify: Reload nginx

- name: Ensure nginx is running
  become: yes
  service:
    name: nginx
    state: started

- stat:
    path: "/var/lib/acme/live/{{ prometheus_domain }}/privkey"
  become: yes
  register: prometheus_key_file_stat

- name: Let acmetool generate a key and a certificate
  become: yes
  when: not prometheus_key_file_stat.stat.exists
  shell: /usr/bin/acmetool want --batch {{ prometheus_domain }}
  notify: Restart nginx


- name: Ensure prometheus configuration for nginx is enabled
  become: yes
  file: 
    state: link
    dest: /etc/nginx/sites-enabled/prometheus.conf
    src: /etc/nginx/sites-available/prometheus.conf 
  notify: Reload nginx