---
- hosts: proxmoxhosts
  tasks:
  - name: Update all installed packages using APT module
    apt:
      name: '*'
      state: latest
      update_cache: yes
      only_upgrade: yes
    register: apt_update_status

  - name: Remove packages not needed anymore
    apt:
      autoremove: yes

  - name: Check if reboot required
    stat:
      path: /var/run/reboot-required
    register: reboot_required_file

  - name: Reboot if required
    reboot:
    when: reboot_required_file.stat.exists == true
