# Ensures perfact-system-tools is >=21.1.0 and cron job is correctly installed
- name: update perfact-system-tools
  hosts: all !peterlackegroup*
  gather_facts: true

  tasks:

    - name: no changes if not ubuntu
      ansible.builtin.meta: end_host
      check_mode: false
      when: ansible_facts['distribution'] != "Ubuntu"

    - name: no changes if not ubuntu 20.04 or 22.04
      ansible.builtin.meta: end_host
      check_mode: false
      when: ansible_facts['distribution_version'] is version('20.04', '<')

    - name: update repositories cache
      become: true
      ansible.builtin.apt:
        update_cache: true
      when: not ('froneri' in inventory_hostname )

    - name: ensure sufficient version is installed
      become: true
      ansible.builtin.apt:
        name: perfact-system-tools
        state: latest
        update_cache: false
        install_recommends: false

    - name: check if cron file exists
      ansible.builtin.stat:
        path: "/etc/cron.d/perfact-system-tools"
      register: cron_file

    - name: fail if cron file doesnt exist
      ansible.builtin.meta: end_host
      when: cron_file.stat.exists == false

    - name: check if cron file dpkg-dist exists
      ansible.builtin.stat:
        path: "/etc/cron.d/perfact-system-tools.dpkg-dist"
      register: cron_file_dist

    - name: alert admin of changed cron file
      debug:
        msg: "found perfact-system-tools.dpkg-dist in /etc/cron.d/"
      when: cron_file_dist.stat.exists == true
