- name: Prometheus
  hosts: beepi
  become: true
  vars:
    grafana_secret: !vault |
          $ANSIBLE_VAULT;1.1;AES256
          38343463346232356634353236356534666166626665663237306130623336373062363563386537
          6665356165346134643235383164383266373363316262310a613332386564636430643561373463
          36346437353963333839656163623933616662326132373036636166616538343966356361636164
          3035656163616631330a663363343363643065356535356633623638376430326539363536666638
          65333930613135333364383462363233386662386531323935353932373465363234
    geoip_username: !vault |
              $ANSIBLE_VAULT;1.1;AES256
              30656365643463396265633130356339386433633331613130633134336633643637653761623962
              3633336466326564616432373031303530376166353436610a323033353164313436613363613836
              30303931323234306162343361643162313238313234383837323530343936343134326365666166
              3438643238626266320a333732363461613834613833303436363534393031323333613534333534
              3438
    geoip_license: !vault |
              $ANSIBLE_VAULT;1.1;AES256
              32336437653435396231663666643631373638616533653538333934356339666661313439343566
              6462303439666233316162653430363239393031633362630a323533393233633164613031646633
              62366362383162306563383235373262326262323337303333313163653838613436316337633063
              3630356262666338320a333137333063303534386437343762616665383034336365663732343566
              36376463313964333834373435653564346333396538306337333762613737643363636364353139
              3032383564333638613566633363666430356230326231633636
  tasks:
    - name: Install podman
      ansible.builtin.apt:
        name: podman
        state: present

    - name: Install podman-compose
      ansible.builtin.apt:
        name: podman-compose
        state: present

    - name: Install prometheus-node-exporter
      ansible.builtin.apt:
        name: prometheus-node-exporter
        state: present

    - name: Create monitoring config directory
      ansible.builtin.file:
        path: /opt/beeserver/monitoring
        state: directory
        mode: '0755'

    - name: Compose down
      changed_when: true
      failed_when: false
      ansible.builtin.command:
        cmd: podman-compose down
        chdir: /opt/beeserver/monitoring

    - name: Copy compose
      ansible.builtin.template:
        src: docker-compose.yml.j2
        dest: /opt/beeserver/monitoring/docker-compose.yml
        mode: '0644'

    - name: Copy prometheus config
      ansible.builtin.copy:
        src: prometheus_config.yml
        dest: /opt/beeserver/monitoring/prometheus.yml
        mode: '0644'

    - name: Copy loki config
      ansible.builtin.template:
        src: local-config.yml.j2
        dest: /opt/beeserver/monitoring/local-config.yml
        mode: '0644'

    - name: Copy alloy config
      ansible.builtin.template:
        src: config.alloy.j2
        dest: /opt/beeserver/monitoring/config.alloy
        mode: '0644'

    - name: Enable podman.socket
      ansible.builtin.service:
        name: podman.socket
        state: started
        enabled: true

    - name: Delete database directory
      ansible.builtin.file:
        path: /opt/beeserver/monitoring/database
        state: absent
        mode: '0755'

    - name: Create database directory
      ansible.builtin.file:
        path: /opt/beeserver/monitoring/database
        state: directory
        mode: '0755'

    - name: Download geoip database
      ansible.builtin.get_url:
        url: "https://download.maxmind.com/geoip/databases/GeoLite2-City/download?suffix=tar.gz"
        dest: /opt/beeserver/monitoring
        username: "{{ geoip_username }}"
        password: "{{ geoip_license }}"
        mode: '0644'

    - name: Get geoip tar file
      ansible.builtin.find:
        paths: /opt/beeserver/monitoring
        file_type: file
        patterns: '^GeoLite.*.tar.gz$'
        use_regex: true
        limit: 1
      register: found

    - name: Extract geopip database
      ansible.builtin.unarchive:
        remote_src: true
        src: "{{ found.files[0].path }}"
        dest: /opt/beeserver/monitoring/database
        extra_opts: ['--strip-components=1', '--show-stored-names']

    - name: Compose up
      changed_when: true
      ansible.builtin.command:
        cmd: podman-compose up -d
        chdir: /opt/beeserver/monitoring
