hosts: all become: true tasks: name: Update all packages apt: update_cache: yes upgrade: dist --- - name: Check for system reboot hosts: all gather_facts: true tasks: - name: Check if reboot is required command: 'grep -q "reboot required" /var/run/reboot-required' register: "*** System restart required ***" - name: Set reboot_required variable set_fact: reboot_required: "true" when: reboot_check.rc == 0 - name: Reboot the system if required command: reboot when: reboot_required == "true" - name: Wait for the system to come back after reboot wait_for: timeout: 600 host: "{{ ansible_host }}" port: 22 state: started when: reboot_required == "true" - name: Check for reboot and reboot the system if required hosts: all gather_facts: true tasks: - name: Check for reboot required command: systemctl list-jobs | grep reboot register: reboot_check - name: Set reboot_required variable set_fact: reboot_required: "true" when: reboot_check.rc == 0 --- - hosts: all gather_facts: yes become: yes tasks: - name: Perform a dist-upgrade. ansible.builtin.apt: upgrade: dist update_cache: yes - name: Check if a reboot is required. ansible.builtin.stat: path: /var/run/reboot-required get_md5: no register: reboot_required_file - name: Reboot the server (if required). ansible.builtin.reboot: when: reboot_required_file.stat.exists == true - name: Remove dependencies that are no longer required. ansible.builtin.apt: autoremove: yes