### variable
variable "vm-subnets" {
type = list(string)
default = ["7.0.1.0/24","7.0.2.0/24","7.0.3.0/24"]
}
### subnet
resource "azurerm_subnet" "task_subnet" {
name = "subnet-${format("%02d",count.index)}"
resource_group_name = azurerm_resource_group.task.name
virtual_network_name = azurerm_virtual_network.task_vnet.name
network_security_group_id = azurerm_network_security_group.task_nsg.id
address_prefix = var.vm-subnets[count.index]
count = length(var.vm-subnets)
}
### NIC
resource "azurerm_network_interface" "vm_nic" {
name = "nic--${format("%02d",count.index)}"
location = var.region
resource_group_name = azurerm_resource_group.task.name
count = var.vm-count
ip_configuration {
name = "${var.resource_prefix}-${format("%02d",count.index)}-ip"
subnet_id = azurerm_subnet.task_subnet.*.id[count.index]
private_ip_address_allocation = "dynamic"
public_ip_address_id = azurerm_public_ip.task_public_ip.*.id[count.index]
}<br>
}对于类似的子网-A=2 3VMs,子网-B=2 3VMs,子网-C=3 3VMs或随机错误:错误:无效索引,我需要将VM分为3个子网。
在vm-network.tf第11行中,在资源"azurerm_network_interface“”vm_nic“中: 11: subnet_id = azurerm_subnet.task_subnet.*.idcount.index x
给定的键不标识此集合值中的元素。
错误:无效索引
在vm-network.tf第11行中,在资源"azurerm_network_interface“”vm_nic“中: 11: subnet_id = azurerm_subnet.task_subnet.*.idcount.index x
The given key does not identify an element in this collection value.
What modification can be done to resolve it and how can I assign different/random subnet on each vm rather then count loop.
I also try to do it using random_shuffle and set-product function but not get the desired output .. please Help 发布于 2020-01-25 09:13:12
经过两天的逻辑斗争,我终于找到了一个解决方案或我创建的问题:使用元素函数https://www.terraform.io/docs/configuration/functions/element.html
NIC
resource "azurerm_network_interface" "vm_nic" {
name = "nic--${format("%02d",count.index)}"
location = var.region
resource_group_name = azurerm_resource_group.task.name
count = var.vm-count
ip_configuration {
name = "${var.resource_prefix}-${format("%02d",count.index)}-ip"
subnet_id = element(azurerm_subnet.task_subnet.*.id, count.index)
private_ip_address_allocation = "dynamic"
public_ip_address_id = azurerm_public_ip.task_public_ip.*.id[count.index]
}
} subnet_id =元素(azurerm_subnet.task_subnet.*.id,count.index)
在vm count.index =3时,使用元素函数"7.0.1.0/24“、"7.0.2.0/24”、"7.0.3.0/24“,返回到子网索引=0,以便在vm.count = 5、7、10.✌下如何工作和测试。
https://stackoverflow.com/questions/59878003
复制相似问题