terraform { required_providers { libvirt = { source = "dmacvicar/libvirt" version = "~> 0.7.0" } } } # Configure the LibVirt Provider provider "libvirt" { uri = "qemu:///system" } # Download Ubuntu cloud image resource "libvirt_volume" "base_image" { name = "ubuntu-22.04-base.qcow2" pool = var.pool_name source = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img" format = "qcow2" } # Create cloud-init disk resource "libvirt_cloudinit_disk" "cloudinit" { name = "cloudinit.iso" pool = var.pool_name user_data = file("${path.module}/cloud-init/user-data.yaml") meta_data = file("${path.module}/cloud-init/meta-data.yaml") } # Create VM volumes from base image resource "libvirt_volume" "vm_disk" { count = var.vm_count name = "vm-${count.index + 1}-disk.qcow2" pool = var.pool_name base_volume_id = libvirt_volume.base_image.id size = var.disk_size } # Create VMs resource "libvirt_domain" "vm" { count = var.vm_count name = "vm-${count.index + 1}" memory = var.memory vcpu = var.vcpu cloudinit = libvirt_cloudinit_disk.cloudinit.id network_interface { network_name = "default" wait_for_lease = true } disk { volume_id = libvirt_volume.vm_disk[count.index].id } console { type = "pty" target_port = "0" target_type = "serial" } graphics { type = "spice" listen_type = "address" autoport = true } }