---

- hosts: "{{ hosts_target }}"
  become: false
  gather_facts: true
  vars_files:
    - /root/.secret/.ansible/vars_creds.yml
  tasks:
    - name: Upgrage system type of Debian using apt
      block:
        - name: Run "apt update" and "apt upgrade"
          ansible.builtin.apt:
            upgrade: yes
            update_cache: yes
#        - name: Check system for need to reboot after update (kernel)
#          stat:
#            path: /var/run/reboot-required
#          register: file_stat_result
#        - name: Debug massage if need to reboot after update (kernel)
#          debug:
#            msg: "{{ ansible_fqdn }} need to reboot after update"
#          when:  file_stat_result.stat.exists
#        - name: Reboot system if kernel updated and requested by the system
#          shell: sleep 5 && /sbin/shutdown -r now 'Rebooting system to update system /kernel as needed'
#          async: 300
#          poll: 0
#          ignore_errors: true
#          when:  file_stat_result.stat.exists
#        - name: Wait for system to become reachable again
#          wait_for_connection:
#              delay: 20
#              timeout: 300
#          when:  file_stat_result.stat.exists
        - name: Run "apt autoremove"
          ansible.builtin.apt:
            autoremove: yes
        - name: Run "apt-get clean"
          ansible.builtin.apt:
            clean: yes
      when: ansible_os_family == "Debian"