variable "vms" { type = list(map(any)) } terraform { required_providers { libvirt = { source = "dmacvicar/libvirt" } } } provider "libvirt" { uri = "qemu:///system" } resource "libvirt_network" "swarm" { name = "swarm" mode = "nat" addresses = ["10.2.3.0/24"] autostart = true dhcp { enabled = false } dns { enabled = true } } resource "libvirt_volume" "os_image" { for_each = { for vm in var.vms : vm.name => vm } name = each.value["os_image_name"] pool = each.value["storage_pool"] source = each.value["os_image_url"] format = "qcow2" } resource "libvirt_volume" "os_datas" { for_each = { for vm in var.vms : vm.name => vm } name = each.value["os_datas_name"] base_volume_id = libvirt_volume.os_image[each.key].id pool = each.value["storage_pool"] size = each.value["disksize"] * 1024 * 1024 * 1024 } data "template_file" "user_data" { for_each = { for vm in var.vms : vm.name => vm } template = file("${path.module}/${each.value["user_data"]}") vars = { hostname = each.value["hostname"] } } data "template_file" "network_config" { for_each = { for vm in var.vms : vm.name => vm } template = file("${path.module}/${each.value["network_config"]}") vars = { network_ip = each.value["network_ip"] } } resource "libvirt_cloudinit_disk" "cloudinit" { for_each = { for vm in var.vms : vm.name => vm } name = "${each.key}-cloudinit.iso" user_data = data.template_file.user_data[each.key].rendered network_config = data.template_file.network_config[each.key].rendered pool = each.value["storage_pool"] } resource "libvirt_domain" "domain" { for_each = { for vm in var.vms : vm.name => vm } name = each.value["name"] memory = each.value["memory"] vcpu = each.value["cpu"] cpu { mode = "host-passthrough" } cloudinit = libvirt_cloudinit_disk.cloudinit[each.key].id network_interface { network_name = each.value["network_name"] } console { type = "pty" target_port = "0" target_type = "serial" } console { type = "pty" target_type = "virtio" target_port = "1" } disk { volume_id = libvirt_volume.os_datas[each.key].id } graphics { type = "spice" listen_type = "address" autoport = true } }