- name: Omv Setup
  become: "{{ false if inventory_hostname == 'nas.home.lan' else true }}"
  block:
    # - name: Gather facts
    #   ansible.builtin.setup:
    # - name: Print
    #   ansible.builtin.debug:
    #     msg: "{{ ansible_facts }}"
    - name: Include vault
      ansible.builtin.include_vars:
        file: jaydee.yml
        name: mysecrets
      when: inventory_hostname != 'nas.home.lan'
    - name: Delete content & directory
      ansible.builtin.file:
        state: absent
        path: "{{ dest_folder }}"

    - name: "Check if listed package is installed or not on Debian Linux family"
      ansible.builtin.package:
        name: "{{ item }}"
        state: present
      check_mode: true
      loop:
        - git
      register: git_installed

    - name: Include role only if missing
      ansible.builtin.include_role:
        name: git
      when: not git_installed

    - name: Pull repo
      tags:
        - git_pull
      ansible.builtin.git:
        repo: "https://{{ mysecrets['git_user'] | urlencode }}:{{ mysecrets['git_password_mqtt'] | urlencode }}@gitlab.sectorq.eu/jaydee/proxmox.git"
        dest: "{{ dest_folder }}"
        update: true
        clone: true
        version: main
      when: inventory_hostname != 'nas.home.lan'
    - name: Print
      ansible.builtin.debug:
        msg: "{{ inventory_hostname }}"
    - name: Create a directory if it does not exist
      ansible.builtin.file:
        path: /myapps/proxmox
        state: directory
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname != 'nas.home.lan'

    - name: Upload script
      ansible.builtin.copy:
        src: "{{ dest_folder }}/proxmox.py"
        dest: /myapps/proxmox/proxmox.py
        remote_src: true
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname != 'nas.home.lan'
    - name: Upload exclude file
      ansible.builtin.copy:
        src: "{{ dest_folder }}/exclude.txt"
        dest: /myapps/exclude.txt
        remote_src: true
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname != 'nas.home.lan'
    - name: Upload requirements
      ansible.builtin.copy:
        src: "{{ dest_folder }}/requirements.txt"
        dest: /myapps/requirements.txt
        remote_src: true
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname != 'nas.home.lan'
    - name: Upload docker_backups.py
      ansible.builtin.copy:
        src: "{{ dest_folder }}/docker_backups.py"
        dest: /myapps/docker_backups.py
        remote_src: true
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname == 'm-server.home.lan'
    - name: Install venv
      ansible.builtin.apt:
        name:
          - python3-virtualenv
          - rsync

    - name: Install specified python requirements in indicated (virtualenv)
      ansible.builtin.pip:
        requirements: /myapps/requirements.txt
        virtualenv: /myapps/venv

    - name: 'Ensure an old job is no longer present. Removes any job that is prefixed by "#Ansible: an old job" from the crontab'
      ansible.builtin.cron:
        name: "omv_backup"
        state: absent
    - name: Upload service config
      ansible.builtin.copy:
        src: omv_backup.service
        dest: /etc/systemd/system/omv_backup.service
        mode: '0755'
        owner: root
        group: root
      when: inventory_hostname == 'amd.home.lan'
    - name: Restart omv service
      ansible.builtin.service:
        name: omv_backup
        state: restarted
        daemon_reload: true
        enabled: true
      when: inventory_hostname == 'amd.home.lan'
    # - name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
    #   ansible.builtin.cron:
    #     name: "omv_backup"
    #     minute: "0"
    #     hour: "8"
    #     job: "sudo /myapps/omv_backup.py -b > /dev/null  2>&1 &"
    #     state: present
