繁体   English   中英

我创建了 4 个子网并创建了 output 值以在一个变量中获取所有子网 id。那么我如何计划检索 2 个值以附加 nics

[英]I have created 4 subnets and created output values to get all subnets ids in one variable .So how can I plan to retrieve 2 values to attach the nics

错误:括号不平衡

on.terraform\modules\nics\main.tf 第 19 行,在资源“azurerm_network_interface”“NIC1”中:19:subnet_id = “${element(var.subnetwork-subnetid.*.id, (0,1))}”

子网的 output 值:

output "subnetwork-subnetid" {
  value = concat(azurerm_subnet.subnetwork.*.id, azurerm_subnet.subnetwork6.*.id)
}

网卡.tf

resource "azurerm_network_interface" "NIC1" {
  #count = "${length(var.subnetwork-subnetid)}"
  #for_each= toset(var.subipv4)
  count = "${length(var.subipv4)}"
  name  = "${lookup(element(var.subipv4, count.index), "name")}"
  #name                = var.nic-name
  location                      = var.rg-location
  resource_group_name           = var.rg-name
  enable_ip_forwarding          = true
  enable_accelerated_networking = true
  ip_configuration {
    name                          = "ipconfig"
    subnet_id                     = "${element(var.subnetwork-subnetid.*.id, (0,1))}"
    private_ip_address_allocation = "Dynamic"
    #public_ip_address_id          = azurerm_public_ip.pubip.id
    #public_ip_address_id = azurerm_public_ip.pubip.*.id
    primary = true
  }
  tags = {
    name = "${lookup(element(var.subipv4, count.index), "name")}"
  }
}```

Please someone help me in this issue.Thanks!

element中的第二个参数是index

index 查找特定元素值的索引。

因此,要根据索引从列表中获取少量元素,您可以执行以下操作:

subnet_id = [ for idx in [0, 1]: element(var.subnetwork-subnetid.*.id, idx) ]

如果你想要一系列独立,你可以使用slice

subnet_id = slice(var.subnetwork-subnetid.*.id, 0, 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM