 - name: Comparing last updated kernel and running kernel
    shell: |
      LAST_KERNEL=$(rpm -q --last kernel |head -1 | awk '{print $1}' | sed 's/kernel-//');
      CURRENT_KERNEL=$(uname -r)

      if [[ $LAST_KERNEL != $CURRENT_KERNEL ]]; then
        # Set reboot flag
        touch /tmp/reboot
        echo "KERNEL_CHANGED"
      else
        echo "No KERNEL Change Detected"
      fi
    register: kernel_update_status

  - name: Reboot Requirement
    debug:
       msg: "{{ kernel_update_status.stdout }}"

  - name: Reboot the machine (Wait for 5 min or 300 Sec)
    reboot:
      reboot_timeout: 300
      test_command: uptime
    when: kernel_update_status.stdout == "KERNEL_CHANGED"
    register: reboot_status

  - name: Machine after reboot
    debug:
       msg: "{{ reboot_status }}"