- name: Setup gitlab runner on the Jumphost
  hosts: jumphost
  become: true
  ignore_errors: "{{ ansible_check_mode }}"
  vars: {}
  tasks:
    - name: define rhel 9 vm with specific specs
      community.libvirt.virt:
        command: define
        xml: |
          <domain type='kvm'>
            <name>gitlab-runner-vm</name>
            <memory unit='MiB'>16384</memory>  <!-- 16 GiB RAM -->
            <vcpu placement='static'>2</vcpu>  <!-- 2 vCPUs -->
            <os>
              <type arch='x86_64' machine='pc'>hvm</type>
              <boot dev='hd'/>
            </os>
            <devices>
              <disk type='file' device='disk'>
                <driver name='qemu' type='qcow2'/>
                <source file='/var/lib/libvirt/images/gitlab-runner.qcow2'/>
                <target dev='vda' bus='virtio'/>
              </disk>
              <interface type='network'>
                <mac address='52:54:00:6b:3c:58'/>
                <source network='default'/>
                <model type='virtio'/>
              </interface>
              <graphics type='vnc' port='-1' autoport='yes'/>
            </devices>
          </domain>
        state: running

    - name: install and configure gitlab runner, docker dependencies using virt-customize
      command: >
        virt-customize -a /var/lib/libvirt/images/gitlab-runner.qcow2
        --run-command "dnf install -y dnf-plugins-core"
        --run-command "dnf config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo"
        --run-command "dnf install -y docker-ce docker-ce-cli containerd.io"
        --run-command "systemctl enable --now docker"
        --run-command "curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh -o /tmp/gitlab-runner-install.sh"
        --run-command "chmod +x /tmp/gitlab-runner-install.sh && /tmp/gitlab-runner-install.sh"
        --run-command "rpm --import https://packages.gitlab.com/runner/gitlab-runner/gpgkey/runner-gitlab-runner-49F16C5CC3A0F81F.pub.gpg"
        --run-command "dnf install -y gitlab-runner"
        --run-command "systemctl enable --now gitlab-runner"
        --run-command "usermod -aG docker gitlab-runner"

  