【问题标题】:Terraform: Iterating and skipping over specific index valuesTerraform:迭代和跳过特定的索引值
【发布时间】:2021-08-04 05:59:22
【问题描述】:

我正在尝试更新我现有的 azurerm_subnet terraform 代码 被许多其他人消耗掉了。所以我们真的不喜欢破坏任何现有的tfvars

network_security_group_id 的折旧字段从azurerm_subnet 转换为名为@9​​87654326@ 的新资源,从版本2.0 开始。

现有代码

resource "azurerm_subnet" "generic_subnet" {
  count = "${length(var.subnet_names)}"

  name = "${element("${var.subnet_names}", count.index)}"
  resource_group_name = "${var.resource_group_name}"
  virtual_network_name = "${azurerm_virtual_network.generic_vnet.name}"
  address_prefix = "${element("${var.subnet_address_ranges}", count.index)}"
  service_endpoints = "${var.service_endpoints[count.index]}"


  network_security_group_id = "${element(var.subnet_nsg_ids, count.index)}"
  route_table_id = "${element(var.subnet_route_tables, count.index)}"
}

tfvars:


subnet_address_ranges = [
 "10.102.40.0/22",
 "10.102.44.0/24", 
 "10.102.45.0/25"
]
subnet_names = [
 "private-subnet-01",
 "public-subnet-01", 
 "protected-subnet-01"
 ]
service_endpoints = [    
    ["Microsoft.AzureCosmosDB","Microsoft.KeyVault", "Microsoft.Storage","Microsoft.Sql","Microsoft.AzureActiveDirectory","Microsoft.ContainerRegistry","Microsoft.EventHub","Microsoft.ServiceBus","Microsoft.Web"],
    [],
    ["Microsoft.KeyVault", "Microsoft.Storage"]
]
subnet_nsg_ids = [
    "/subscriptions/yyyy/resourceGroups/yyy/providers/Microsoft.Network/networkSecurityGroups/nsg-01",
    "",
"/subscriptions/yyyy/resourceGroups/yyy/providers/Microsoft.Network/networkSecurityGroups/nsg-02" 
]

更新代码(通过引入azurerm_network_interface_security_group_association 资源)

resource "azurerm_subnet" "generic_subnet" {
  count = "${length(var.subnet_names)}"

  name = "${element("${var.subnet_names}", count.index)}"
  resource_group_name = "${var.resource_group_name}"
  virtual_network_name = "${azurerm_virtual_network.generic_vnet.name}"
  address_prefix = "${element("${var.subnet_address_ranges}", count.index)}"
  service_endpoints = "${var.service_endpoints[count.index]}"


  network_security_group_id = "${element(var.subnet_nsg_ids, count.index)}"
  route_table_id = "${element(var.subnet_route_tables, count.index)}"
}

resource "azurerm_subnet_network_security_group_association" "generic_nsg_association" {
  count= "${length(var.subnet_nsg_ids)}" 
  subnet_id = "${element(azurerm_subnet.generic_subnet.*.id, count.index)}"
  network_security_group_id = "${element(var.subnet_nsg_ids, count.index)}"
}

显然我的新资源会通过抛出类似的错误来破坏所有 tf​​vars

错误:无法将“network_security_group_id”解析为资源 ID: 无法解析 Azure ID:解析“”:空 url

在 ../../../main.tf 第 41 行,在资源中 “azurerm_subnet_network_security_group_association” “generic_nsg_association”:41:network_security_group_id = "${element(var.subnet_nsg_ids, count.index)}"

原因是我们需要给network_security_group_id参数。但是,我所有现有的 tfvar 都有一个结构支持 subnet_nsg_ids 列表和一个空字符串。

所以我的问题是有没有办法只循环遍历列表中我的azurerm_subnet_network_security_group_association 资源的特定索引。例如,仅循环通过 [0] 和 [2] 但跳过 [1](因为 [1] 是一个空字符串

我不需要只使用count。如果这种跳过是可能的,我也很乐意使用for_each

【问题讨论】:

  • 但是subnet_names 怎么办?你有三个。 public-subnet-01 会发生什么?
  • public-subnet-01 在第一次运行时不与任何 nsg 规则相关联。我们将在单独的 tf 文件中创建 nsg,并稍后在此处提供 id 以与子网关联。
  • 我与我的团队交谈,他们建议我重写一个新模块并将其与azurerm_subnet_network_security_group_association 分开以便更好地处理。因为这使得处理来自 terraform 的未来重大版本变得更加复杂。不管怎样,谢谢你的回答,我会接受的

标签: terraform terraform-provider-azure


【解决方案1】:

我不确定我是否完全理解您的问题,但如果您想在有空字符串时跳过您的 network_security_group_id,您可以使用:

network_security_group_id = element(var.subnet_nsg_ids, count.index) != "" ? element(var.subnet_nsg_ids, count.index) : null

如果你想过滤掉带有空字符串的项目,你可以这样做:

for_each = [for idx, val in var.subnet_names): val if var.subnet_nsg_ids[idx] != ""]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-09
    • 2018-12-04
    • 2014-08-30
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多