# vim main.tf # Definindo variáveis para configuração da VM variable "vm_template" { type = map(any) } terraform { required_providers { libvirt = { source = "dmacvicar/libvirt" } } } provider "libvirt" { uri = "qemu:///system" } # Volume de imagem do sistema operacional resource "libvirt_volume" "os_image" { name = var.vm_template["os_image_name"] pool = var.vm_template["storage_pool"] source = var.vm_template["os_image_url"] format = "qcow2" } # Volume de dados da VM resource "libvirt_volume" "os_datas" { name = var.vm_template["os_volume_name"] base_volume_id = libvirt_volume.os_image.id pool = var.vm_template["storage_pool"] size = var.vm_template["disksize"] * 1024 * 1024 * 1024 # Converte para bytes } # Definir o arquivo cloud-init para automação data "template_file" "user_data" { template = file("${path.module}/${var.vm_template["cloud_init_file"]}") vars = { hostname = var.vm_template["hostname"] ssh_key = var.vm_template["ssh_key"] } } # Criando disco Cloud-Init resource "libvirt_cloudinit_disk" "cloudinit" { name = "cloudinit.iso" user_data = data.template_file.user_data.rendered pool = var.vm_template["storage_pool"] } # Criando a VM com libvirt resource "libvirt_domain" "domain" { name = var.vm_template["name"] memory = var.vm_template["memory"] vcpu = var.vm_template["cpu"] cpu { mode = "host-passthrough" } cloudinit = libvirt_cloudinit_disk.cloudinit.id network_interface { network_name = var.vm_template["network_name"] wait_for_lease = true } console { type = "pty" target_type = "virtio" target_port = "1" } disk { volume_id = libvirt_volume.os_datas.id } graphics { type = "spice" listen_type = "address" autoport = true } } output "ip" { value = libvirt_domain.domain.network_interface[0].addresses[0] }