diff --git a/terraform/example/cloud-init-with-loop.tf b/terraform/example/cloud-init-with-loop.tf new file mode 100644 index 0000000000000000000000000000000000000000..15e9d5be7ec3f33b84716d04729a99ae27d4c386 --- /dev/null +++ b/terraform/example/cloud-init-with-loop.tf @@ -0,0 +1,94 @@ +# Main configuration for our project. +# This example is quite overkill, this is an example that +# iterates over a list of vms and reuses the same resource object. + +# You don't have to use this file, but I recommend it. +# Larger example: https://git.sch.bme.hu/kszk/sysadmin/kubernetes/cluster-setup/-/tree/master/terraform + +locals { + vm_map = { + example-vm = { + num_cpus = 1 + memory = 1024 + template_uuid = data.vsphere_virtual_machine.ubuntu2004-cloud-init.id + datastore_cluster_id = data.vsphere_datastore_cluster.SCH-Cluster-FujiStorage.id + vapp_properties = { + public-keys = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPpH+TNAwcmxYc5cVctH04wUU83Pba6s/AkKXOnhDn+m rlacko@zen" + user-data = base64encode(file("${path.module}/cloud-init/example-vm.yaml")) + } + disk_map = { + 0 = { size = 16 } + } + network_interface_map = { + 01 = { + id = data.vsphere_network.dvPG-208-Szerver.id + mac_address = "aa:aa:aa:aa:aa:aa" + } + } + } + } +} + +resource "vsphere_virtual_machine" "vm" { + for_each = { for k, v in local.vm_map : k => v } + + name = each.key + resource_pool_id = data.vsphere_resource_pool.kubernetes.id + guest_id = try(each.value.guest_id, "ubuntu64Guest") + num_cpus = try(each.value.num_cpus, 1) + memory = try(each.value.memory, 1024) + firmware = try(each.value.firmware, "efi") + folder = "KSZK/sysadmin/k8s" + wait_for_guest_net_timeout = try(each.value.wait_for_guest_net_timeout, 0) + wait_for_guest_ip_timeout = try(each.value.wait_for_guest_ip_timeout, 0) + sync_time_with_host = true + sync_time_with_host_periodically = false + + datastore_cluster_id = try(each.value.datastore_cluster_id, null) + datastore_id = try(each.value.datastore_id, null) + + dynamic "clone" { + for_each = can(each.value.template_uuid) ? [1] : [] + content { + template_uuid = each.value.template_uuid + } + } + dynamic "vapp" { + for_each = can(each.value.template_uuid) ? [1] : [] + content { + properties = { + public-keys = each.value.vapp_properties.public-keys + hostname = try(each.value.vapp_properties.hostname, each.key) + instance-id = try(each.value.vapp_properties.instance-id, each.key) + password = try(each.value.vapp_properties.password, null) + user-data = try(each.value.vapp_properties.user-data, null) + } + } + } + + dynamic "network_interface" { + for_each = each.value.network_interface_map + content { + network_id = network_interface.value.id + use_static_mac = can(network_interface.value.mac_address) + mac_address = try(network_interface.value.mac_address, null) + } + } + + dynamic "disk" { + for_each = each.value.disk_map + content { + thin_provisioned = false + label = "disk${tostring(disk.key)}" + size = disk.value.size + unit_number = tonumber(disk.key) + } + } + + dynamic "cdrom" { + for_each = can(each.value.template_uuid) ? [1] : [] + content { + client_device = true + } + } +}