variable "vms" { type = map(object( { domain = string cpu = number vcpu = number ram = number system_image_persistent = bool data_disks = list(object({ name = string size = string persistent = bool })) network_interfaces = list(object({ network_id = string ip = string mac = string })) sysdatastore = string } )) description = "Virtual Machines description" } variable "image_format" { default = "qcow2" } variable "dev_prefix" { default = "sd" } variable "permissions" { default = "600" } variable "graphics" { default = { keymap = "fr" listen = "0.0.0.0" type = "VNC" } } variable "one_image_ds" { default = "101" } variable "one_sys_datastore" { default = "100" } locals { data_disks = flatten([ for vm_key, vm in var.vms : [ for disk in vm.data_disks : { vm_key = "${vm_key}.${vm.domain}" name = disk.name size = disk.size persist = disk.persistent } ] ]) } // system image resource "opennebula_image" "system" { for_each = var.vms name = "system-${each.key}.${each.value.domain}" description = "System disk for ${each.key}" datastore_id = var.one_image_ds path = "https://vulcain.cadoles.com/empty_20G" persistent = each.value.system_image_persistent format = var.image_format dev_prefix = var.dev_prefix permissions = var.permissions } // data image not needed now resource "opennebula_image" "data" { for_each = { for ds in local.data_disks : "${ds.vm_key}.${ds.name}" => ds } name = "${each.value.vm_key}-data-${each.value.name}" path = "https://vulcain.cadoles.com/empty_${each.value.size}" datastore_id = var.one_image_ds description = "Data disk of ${each.value.size} for ${each.value.vm_key}" persistent = each.value.persist format = var.image_format dev_prefix = var.dev_prefix permissions = var.permissions } resource "opennebula_template" "vm_template" { for_each = var.vms name = "${each.key}.${each.value.domain}" cpu = each.value.cpu vcpu = each.value.vcpu memory = each.value.ram graphics { keymap = var.graphics.keymap listen = var.graphics.listen type = var.graphics.type } context = { SET_HOSTNAME = "$NAME" DNS_HOSTNAME = "YES" NETWORK = "YES" SSH_PUBLIC_KEY = "$USER[SSH_PUBLIC_KEY]" USERNAME = "root" } disk { image_id = opennebula_image.system[each.key].id } dynamic "disk" { for_each = { for ds in local.data_disks : "${ds.vm_key}.${ds.name}" => ds if "${each.key}.${each.value.domain}" == ds.vm_key } iterator = current_disk content { image_id = opennebula_image.data[current_disk.key].id } } //sched_ds_requirements = ( each.value.sysdatastore != "" ? "ID=\"${each.value.sysdatastore}\"" : "ID=\"${var.one_sys_datastore}\"" ) sched_ds_requirements = ( each.value.sysdatastore != "" ? "ID=\"${each.value.sysdatastore}\"" : "" ) os { arch = "x86_64" boot = "disk0,nic0" } dynamic "nic" { for_each = each.value.network_interfaces content { network_id = nic.value.network_id model = "virtio" mac = ( nic.value.mac != "" ? nic.value.mac : "" ) ip = ( nic.value.ip != "" ? nic.value.ip : "" ) } } permissions = var.permissions depends_on = [ opennebula_image.system ] /* Not needed now depends_on = [ opennebula_image.system, opennebula_image.data ] */ } resource "opennebula_virtual_machine" "vm" { for_each = var.vms name = "${each.key}.${each.value.domain}" template_id = opennebula_template.vm_template[each.key].id depends_on = [ opennebula_template.vm_template ] }