首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将随机subnet_id分配到azurerm_network_interface中?

如何将随机subnet_id分配到azurerm_network_interface中?
EN

Stack Overflow用户
提问于 2020-01-23 11:56:21
回答 1查看 661关注 0票数 1
代码语言:javascript
复制
### 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

代码语言:javascript
复制
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 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-25 09:13:12

经过两天的逻辑斗争,我终于找到了一个解决方案或我创建的问题:使用元素函数https://www.terraform.io/docs/configuration/functions/element.html

NIC

代码语言:javascript
复制
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.✌下如何工作和测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59878003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档