您可以使用simple math interpolation像這樣的東西:
variable "count" {
default = 10
}
resource "azurerm_network_interface" "test" {
name = "${format("VM%02d-NIC1", count.index + 1)}"
location = "West US"
resource_group_name = "myResourceGroup"
count = "${var.count}"
ip_configuration {
name = "${format("ipConfig-VM%02d-NIC1", count.index + 1)}"
subnet_id = "SubNet"
private_ip_address_allocation = "Static"
private_ip_address = "192.168.5.${count.index + 1}"
}
tags {
fault_domain = "${(count.index + 1) % 3}"
}
}
然後根據您的要求建立基礎設施的其餘部分並分配NIC給虛擬機:
resource "azurerm_virtual_machine" "test" {
count = "${var.count}"
name = "${format("VM%02-test", cound.index + 1)}"
location = "West US"
resource_group_name = "myResourceGroup"
network_interface_ids = ["${element(azurerm_network_interface.test, count.index).id}"]
vm_size = "Standard_A0"
...
}