# platform = multi_platform_debian,multi_platform_fedora,multi_platform_ol,multi_platform_rhel,multi_platform_rhv,multi_platform_sle,multi_platform_slmicro,multi_platform_almalinux
# reboot = true
# strategy = restrict
# complexity = medium
# disruption = low



- name: Check slab_nomerge argument exists
  ansible.builtin.command: grep '^\s*GRUB_CMDLINE_LINUX=.*slab_nomerge=' /etc/default/grub
  check_mode: False
  failed_when: False
  changed_when: False
  register: argcheck

- name: Check slab_nomerge argument exists
  ansible.builtin.command: grep '^\s*GRUB_CMDLINE_LINUX=' /etc/default/grub
  check_mode: False
  failed_when: False
  changed_when: False
  register: linecheck

- name: Add slab_nomerge argument
  ansible.builtin.lineinfile:
    line: 'GRUB_CMDLINE_LINUX="slab_nomerge=yes "'
    state: present
    dest: /etc/default/grub
    create: yes
    mode: '0644'
  when: argcheck is not skipped and linecheck is not skipped and argcheck.rc != 0 and linecheck.rc != 0

- name: Replace existing slab_nomerge argument
  ansible.builtin.replace:
      path: /etc/default/grub
      regexp: 'slab_nomerge=[a-zA-Z0-9,]+'
      replace: 'slab_nomerge=yes'
  when: argcheck is not skipped and linecheck is not skipped and argcheck.rc == 0 and linecheck.rc == 0

- name: Add slab_nomerge argument
  ansible.builtin.replace:
      path: /etc/default/grub
      regexp: '(^\s*GRUB_CMDLINE_LINUX=.*)"'
      replace: '\1 slab_nomerge=yes"'
  when: argcheck is not skipped and linecheck is not skipped and argcheck.rc != 0 and linecheck.rc == 0


- name: Check if slab_nomerge argument is already present in /etc/default/grub
  ansible.builtin.slurp:
    src: /etc/default/grub
  register: etc_default_grub

- name: Check if slab_nomerge argument is already present
  ansible.builtin.command: /sbin/grubby --info=ALL
  register: grubby_info
  check_mode: False
  changed_when: False
  failed_when: False

- name: Update grub defaults and the bootloader menu
  ansible.builtin.command: /sbin/grubby --update-kernel=ALL --args="slab_nomerge=yes"
  when: (grubby_info.stdout is not search('slab_nomerge=yes')) or ((etc_default_grub['content'] | b64decode) is not search('slab_nomerge=yes'))

