I’m learning Terraform and I do have a question: How to create and assign static IP range in Terraform + vSphere? Today I have a range of static IPs that can be used/assigned to the VMs (e.g. 192.168.25.10/24 - 192.168.25.50/24). I’d like to assign those IPs when configuring environment via Terraform based on number of VMs to be created. If I create 5 VMs, the IP to be used will be 192.168.25.10 - 14. Is that possible? I can do something like that today, but it is a manual and painful process (see below), especially if I decide to spin 30+ VMs. Hopefully there is a better way to do this.
#How it is configured today
#terraform.tfvars
vm_ips = {
"0" = "192.168.25.10"
"1" = "192.168.25.11"
"2" = "192.168.25.12"
"3" = "192.168.25.13"
"4" = "192.168.25.14"
}
#variables.tf
variable "vm_ips" {
type = map(any)
description = "List of IPs used for the Vms"
}
#main.tf
resource "vsphere_virtual_machine" "vms" {
count = length(var.vm_ips)
name = "${var.guest_name_prefix}-vm0${count.index + 1}"
resource_pool_id = data.vsphere_compute_cluster.target_cluster.resource_pool_id
datastore_id = data.vsphere_datastore.target_datastore.id
folder = var.deploy_vsphere_folder
...
network_interface {
ipv4_address = lookup(var.vm_ips, count.index)
ipv4_netmask = var.guest_ipv4_netmask