- name: Setup gitlab runner on the Jumphost
  hosts: jumphost
  become: true
  ignore_errors: "{{ ansible_check_mode }}"
  vars: {}
  tasks:
    - name: define rhel vm
      community.libvirt.libvirt_domain:
        command: define
        name: gitlab-runner-vm
        state: running
        memory: 16384  # 16GB RAM in MiB
        vcpu: 2  # 2 CPU cores
        disk:
          - size: 50G  # 50GB disk
            type: qcow2
            path: /var/lib/libvirt/images/gitlab-runner.qcow2
        networks:
          - name: default
        os_type: linux
        os_variant: rhel8.0  # adjust based on your rhel version
        graphics:
          type: none  # headless

    - name: install rhel on the vm (ISO path should point to your rhel iso)
      community.libvirt.libvirt_domain:
        command: install
        name: gitlab-runner-vm
        state: running
        cdrom: /path/to/rhel.iso
        graphics: vnc
        boot_dev: cdrom

    - name: wait for vm to boot
      wait_for:
        timeout: 600

- name: configure the gitlab runner on the rhel vm
  hosts: gitlab-runner-vm
  become: yes
  tasks:
    - name: enable required repos
      yum:
        name: 'rhel-8-for-x86_64-baseos-rpms'
        state: present

    - name: install gitlab runner
      yum:
        name: gitlab-runner
        state: present

    - name: enable and start gitlab-runner
      systemd:
        name: gitlab-runner
        enabled: yes
        state: started