---
- name: Configre work environment
  become: true
  block:
    - name: Wait for connection
      ansible.builtin.wait_for_connection:
        timeout: 60
        delay: 5

    - name: Gather facts
      ansible.builtin.setup:

    - name: Check if host is booted from the Arch install media
      ansible.builtin.stat:
        path: /run/archiso
      register: archiso_stat

    - name: Abort if the host is not booted from the Arch install media
      ansible.builtin.fail:
        msg: This host is not booted from the Arch install media!
      when: not archiso_stat.stat.exists

    - name: Setect Interface
      ansible.builtin.shell: "set -o pipefail && ip l | awk -F': ' '!/lo/{print $2; exit}'"
      changed_when: interface_name.rc == 0
      register: interface_name

    - name: Set IP-Address
      ansible.builtin.command: "ip addr replace {{ ansible_host }}/{{ vm_nms | default(24) }} dev {{ interface_name.stdout }}"
      changed_when: result.rc == 0
      register: result

    - name: Set Default Gateway
      ansible.builtin.command: "ip route replace default via {{ vm_gw }}"
      changed_when: result.rc == 0
      register: result

    - name: Synchronize clock via NTP
      ansible.builtin.command: timedatectl set-ntp true
      changed_when: result.rc == 0
      register: result

    - name: Configure SSH for root login
      when: vmware_ssh | bool
      block:
        - name: Allow empty passwords temporarily
          ansible.builtin.replace:
            path: /etc/ssh/sshd_config
            regexp: "^#?PermitEmptyPasswords.*"
            replace: "PermitEmptyPasswords yes"

        - name: Allow root login
          ansible.builtin.replace:
            path: /etc/ssh/sshd_config
            regexp: "^#?PermitRootLogin.*"
            replace: "PermitRootLogin yes"

        - name: Reload SSH service to apply changes
          ansible.builtin.service:
            name: sshd
            state: reloaded

        - name: Set connection back to SSH
          ansible.builtin.set_fact:
            ansible_connection: ssh
            ansible_user: "root"
            ansible_password: ""
            ansible_become_password: ""
            ansible_ssh_extra_args: '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'

    - name: Speed-up Bootstrap process
      ansible.builtin.lineinfile:
        path: /etc/pacman.conf
        regexp: ^#ParallelDownloads =
        line: ParallelDownloads = 20

    - name: Wait for Pacman
      ansible.builtin.wait_for:
        timeout: 15

    - name: Setup Pacman
      community.general.pacman:
        update_cache: true
        force: true
        name: "{{ item }}"
        state: latest
      loop:
        - glibc
        - dnf
      retries: 4
      delay: 15

    - name: Prepare /iso mount and repository for RHEL-based systems
      when: os | lower in ["rhel8", "rhel9"]
      block:
        - name: Create /iso directory
          ansible.builtin.file:
            path: /usr/local/install/redhat/dvd
            state: directory
            mode: '0755'

        - name: Mount RHEL ISO
          ansible.posix.mount:
            src: "/dev/sr1"
            path: /usr/local/install/redhat/dvd
            fstype: iso9660
            opts: "ro,loop"
            state: mounted

    - name: Configure RHEL Repos for installation
      block:
        - name: Create directories for repository files and RPM GPG keys
          ansible.builtin.file:
            path: /etc/yum.repos.d
            state: directory
            mode: '0755'

        - name: Create RHEL repository file
          ansible.builtin.template:
            src: "{{ os | lower }}.repo.j2"
            dest: /etc/yum.repos.d/{{ os | lower }}.repo
            mode: '0644'