---
- name: Check for kernel update
  collect_kernel_info:
    lookup_packages: false
  register: __pve_kernel_update
  when: pve_reboot_on_kernel_update

- block:
  - name: Reboot for kernel update
    command: "shutdown -r +1 'PVE kernel update detected by Ansible, rebooting'"
    async: 0
    poll: 0
    ignore_errors: true

  - name: Wait for server to come back online
    local_action: wait_for port=22 host={{ inventory_hostname }} search_regex=OpenSSH delay=60 timeout=450
    become: false

  when:
    - pve_reboot_on_kernel_update
    - __pve_kernel_update.new_kernel_exists

- name: Collect kernel package information
  collect_kernel_info:
  register: __pve_kernel

- name: Remove old Debian/PVE kernels
  apt:
    name: "{{ item }}"
    state: absent
    purge: yes
  with_items: 
    - linux-image-amd64
    - "{{ __pve_kernel.old_packages }}"
  when:
    - pve_remove_old_kernels

---
- name: Check for kernel update
  collect_kernel_info:
    lookup_packages: false
  register: __pve_kernel_update
  when: pve_reboot_on_kernel_update

- block:
  - name: Reboot for networking changes
    shell: "sleep 5 && shutdown -r now 'PVE kernel update detected by Ansible'"
    async: 1
    poll: 0

  - name: Wait for server to come back online
    wait_for_connection:
      delay: 15
  when:
    - pve_reboot_on_kernel_update
    - __pve_kernel_update.new_kernel_exists

- name: Collect kernel package information
  collect_kernel_info:
  register: __pve_kernel

- name: Remove old Debian/PVE kernels
  apt:
    name: "{{ item }}"
    state: absent
    purge: yes
  with_items: 
    - linux-image-amd64
    - "{{ __pve_kernel.old_packages }}"
  when:
    - pve_remove_old_kernels

Use wait_for_connection for reboot and reboot more quickly
Use wait_for_connection for reboot and reboot more quickly
