---
- name: Add grafana keyring
  ansible.builtin.deb822_repository:
    name: grafana
    types: [deb]
    suites: [stable]
    components: [main]
    uris: https://apt.grafana.com
    signed_by: https://apt.grafana.com/gpg.key
    state: present
    enabled: true
  become: true

- name: Install required packages
  ansible.builtin.apt:
    pkg:
      - apt-transport-https
      - software-properties-common
      - wget
      - grafana
      - prometheus
    update_cache: true
  become: true

- name: Start grafana service
  ansible.builtin.systemd_service:
    name: grafana-server
    state: started
  become: true

- name: Start prometheus service
  ansible.builtin.systemd_service:
    name: prometheus
    state: started
  become: true

- name: Copy prometheus config
  ansible.builtin.template:
    src: prometheus.yml.j2
    dest: /etc/prometheus/prometheus.yml
    owner: root
    group: root
    mode: "0644"
  become: true
  notify: Restart prometheus

- name: Copy nginx config
  ansible.builtin.copy:
    src: grafana.conf
    dest: /etc/nginx/sites-enabled/grafana.conf
    mode: "0644"
  become: true

- name: Reload nginx config
  ansible.builtin.service:
    name: nginx
    state: reloaded
  become: true
