--- - hosts: all become: true tasks: - name: Update apt cache and upgrade all packages apt: update_cache: yes upgrade: dist autoremove: yes autoclean: yes - name: Check if a reboot is required ansible.builtin.stat: path: /var/run/reboot-required register: reboot_required_file - name: Reboot the server if required ansible.builtin.reboot: reboot_timeout: 600 when: reboot_required_file.stat.exists == true