---
- name: Make sure the required related facts are available
  ansible.builtin.setup:
    gather_subset:
    - "!all"
    - "!min"
    - kernel

- name: Get kernel version
  ansible.builtin.set_fact:
    initramfs_kernel_version: "{{ ansible_facts.kernel }}"

- name: Get default kernel
  ansible.builtin.command:
    cmd: /sbin/grubby --default-kernel
  register: initramfs_grubby_rc
  changed_when: false
  check_mode: false

- name: Parse default kernel version
  ansible.builtin.set_fact:
    initramfs_default_kernel: "{{ ((((initramfs_grubby_rc.stdout_lines[0] | split('/'))[2] | split('-'))[1:]) | join('-')) | trim }}"

- name: Check the values
  ansible.builtin.assert:
    that: initramfs_default_kernel == initramfs_kernel_version
    fail_msg: "Current kernel version '{{ initramfs_kernel_version }}' is not the default version '{{ initramfs_default_kernel }}'"
    success_msg: "Current kernel version {{ initramfs_kernel_version }} and default version {{ initramfs_default_kernel }} match"
