---
-
  name: "Creating directory for Auto Login in Terminal"
  ansible.builtin.file:
    path: /etc/systemd/system/serial-getty@ttyS0.service.d
    state: directory
    mode: "0755"

-
  name: "Enable Auto Login configuration"
  when: enable_auto_login
  ansible.builtin.lineinfile:
    create: true
    line: "{{ item }}"
    mode: "0644"
    path: /etc/systemd/system/serial-getty@ttyS0.service.d/override.conf
  with_items:
    - "[Service]"
    - ExecStart=
    - "ExecStart=/sbin/agetty --autologin root -8 --keep-baud 115200,38400,9600 ttyS0 $TERM"

-
  name: "Ensuring that a serial terminal is enabled in Grub"
  ansible.builtin.lineinfile:
    path: /etc/default/grub
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
  with_items:
    - { regexp: '^GRUB_CMDLINE_LINUX_DEFAULT=', line: 'GRUB_CMDLINE_LINUX_DEFAULT="quiet"' }
    - { regexp: '^GRUB_CMDLINE_LINUX=', line: 'GRUB_CMDLINE_LINUX="console=tty0 console=ttyS0,115200n8 net.ifnames=0 biosdevname=0"' }

-
  name: "Updating Grub packages"
  ansible.builtin.yum:
    name:
      - grub2-efi-x64
      - grub2-pc
    state: present
    update_cache: true

-
  name: "Installing Grub for BIOS systems"
  ansible.builtin.command: grub2-install --target=i386-pc /dev/sda
  args:
    creates: /boot/grub2/i386-pc

-
  name: "Generating Grub configuration"
  ansible.builtin.command: grub2-mkconfig -o /boot/grub2/grub.cfg
  register: grub_config
  changed_when: grub_config.rc != 0

-
  name: "Creating proper EFI Directory for all BIOS/UEFI Loaders"
  ansible.builtin.file:
    path: /boot/efi/EFI/BOOT
    state: directory
    mode: "0755"

-
  name: "Setting EFI boot executable"
  when: ansible_distribution == 'AlmaLinux'
  ansible.builtin.set_fact:
    grub_efi_src: /boot/efi/EFI/almalinux/grubx64.efi

-
  name: "Setting EFI boot executable"
  when: ansible_distribution == 'RedHat'
  ansible.builtin.set_fact:
    grub_efi_src: /boot/efi/EFI/redhat/grubx64.efi

-
  name: "Copying Grub EFI file"
  become: true
  ansible.builtin.copy:
    src: "{{ grub_efi_src }}"
    dest: /boot/efi/EFI/BOOT/BOOTX64.EFI
    remote_src: true
    mode: "644"
