---
- name: Install extra packages from stable
  ansible.builtin.apt:
    name: "{{ item }}"
  loop:
    - "{{ extra_pkgs }}"
    - "{{ extra_pkgs1 }}"
    - "{{ extra_pkgs2 }}"
  tags:
    - baseinstall

- name: Add backports for {{ ansible_distribution_release }}
  ansible.builtin.apt_repository:
    repo: >
      deb http://deb.debian.org/debian/ {{ ansible_distribution_release }}-backports
      main non-free-firmware
    state: present
    update_cache: true
  when: extra_pkgs_bpo | length > 0 or extra_pkgs_bpo1 | length > 0 or extra_pkgs_bpo2 | length > 0

- name: Install extra packages from backports
  ansible.builtin.apt:
    name: "{{ item }}"
    state: latest # noqa package-latest
    default_release: "{{ ansible_distribution_release }}-backports"
  loop:
    - "{{ extra_pkgs_bpo }}"
    - "{{ extra_pkgs_bpo1 }}"
    - "{{ extra_pkgs_bpo2 }}"
  when: extra_pkgs_bpo | length > 0 or extra_pkgs_bpo1 | length > 0 or extra_pkgs_bpo2 | length > 0


- name: Check if former ansible-stamp exists
  ansible.builtin.stat:
    path: /var/local/ansible-stamps
  register: stamp_exists

- name: Trigger Reboot if no former ansible-run is found
  ansible.builtin.debug:
    msg: "First Ansible-Run on Client - Reboot handler started"
  changed_when: not stamp_exists.stat.exists
  notify: "Reboot client"

- name: Timestamp successfull run and send up-to-date report
  ansible.builtin.shell:
    cmd: date --iso-8601=seconds >> /var/local/ansible-stamps && /usr/local/sbin/reporter
  changed_when: false
  tags: upgrade
