0

我正在尝试从我的子网名称列表中创建一个映射列表,因此我创建了以下内容:

  1. 创建了一个名为 subnet_names 的变量,类型为字符串列表
  2. 创建了一个空资源块以从此列表中创建地图列表,如下所示:
resource "null_resource" "subnet_mapping" {
   count = "${length(var.subnet_names)}"

   triggers = {
    name   = "${element(var.subnet_names, count.index)}"
    number = "${count.index}"
  }
}

如果我只执行此块,我的地图列表正确,但是当我尝试将此地图列表与动态块一起使用时,这是行不通的。

resource "azurerm_virtual_network" "virtual_network" {
  address_space = "${var.cidr_network_range}"
  location = "${var.location}"
  name = "${var.virtual_network_resource_name}"
  resource_group_name = "${var.resource_group_name}"
  count = "${length(var.subnet_names)}"

  dynamic "subnet"{
    for_each = [for s in null_resource.subnet_mapping: {
      name = s.name
      prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,s.number)
    }]

    content {
      name = subnet.value.name
      address_prefix = subnet.value.prefix
    }
  }

  depends_on = [null_resource.subnet_mapping]
}

resource "null_resource" "subnet_mapping" {
  count = "${length(var.subnet_names)}"

  triggers = {
    name   = "${element(var.subnet_names, count.index)}"
    number = "${count.index}"
  }
}


resource "azurerm_resource_group" "virtual_network_group" {
  location = "${var.location}"
  name = "${var.resource_group_name}"
}

它应该是有效的,但我仍然没有 null_resource 的输出,所以它失败了

dynamic "subnet"{
    for_each = [for s in null_resource.subnet_mapping: {
      name = s.name
      prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,s.number)
    }]

    content {
      name = subnet.value.name
      address_prefix = subnet.value.prefix
    }
  }

  depends_on = [null_resource.subnet_mapping]
}

我的错误信息:

Error: Unsupported attribute

  on main.tf line 10, in resource "azurerm_virtual_network" "virtual_network":
  10:       name = s.name

This object does not have an attribute named "name".


Error: Unsupported attribute

  on main.tf line 10, in resource "azurerm_virtual_network" "virtual_network":
  10:       name = s.name

This object does not have an attribute named "name".


Error: Unsupported attribute

  on main.tf line 10, in resource "azurerm_virtual_network" "virtual_network":
  10:       name = s.name

This object does not have an attribute named "name".


Error: Unsupported attribute

  on main.tf line 10, in resource "azurerm_virtual_network" "virtual_network":
  10:       name = s.name

This object does not have an attribute named "name".


Error: Unsupported attribute

  on main.tf line 11, in resource "azurerm_virtual_network" "virtual_network":
  11:       prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,s.number)

This object does not have an attribute named "number".


Error: Unsupported attribute

  on main.tf line 11, in resource "azurerm_virtual_network" "virtual_network":
  11:       prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,s.number)

This object does not have an attribute named "number".


Error: Unsupported attribute

  on main.tf line 11, in resource "azurerm_virtual_network" "virtual_network":
  11:       prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,s.number)

This object does not have an attribute named "number".


Error: Unsupported attribute

  on main.tf line 11, in resource "azurerm_virtual_network" "virtual_network":
  11:       prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,s.number)

This object does not have an attribute named "number".

4

2 回答 2

0

我这样解决了,我创建了一个名为

variable "subnets" {
  type = list(map(string))
  description = "A list of maps of names and network addresses bits for subnets that will be created inside this network (this is parallel to subnet_prefixes array)."
}
resource "azurerm_virtual_network" "virtual_network" {
  address_space = var.cidr_network_range
  location = var.location
  name = var.virtual_network_resource_name
  resource_group_name = var.resource_group_name

  dynamic "subnet"{
    for_each = [for subnet in var.subnets: {
      name = subnet.name
      prefix = cidrsubnet(element(var.cidr_network_range, 0),8 ,subnet.number)
    }]
    content {
      name = subnet.value.name
      address_prefix = subnet.value.prefix
    }
  }
}

resource "azurerm_resource_group" "virtual_network_group" {
  location = var.location
  name = var.resource_group_name
}

这样我已经创建了地图,我想像我之前发布的那样自动创建它,但它在 terraform 方式中似乎并不常见。

Charles Xu示例中,映射将基于序列 (1,2,3) 创建,我想始终以不同的方式使用它,例如 (21,24,26) 所以我能找到的唯一解决方案是强制之前的地图。

总之谢谢大家

于 2020-02-10T10:31:45.013 回答
0

从消息中,我不清楚您要创建多少个 Vnet,因为您的代码有点混乱。对于你的问题null_resource,我认为这不是一个好的选择,我推荐locals.

在这里,我假设您只想创建一个带有串行子网的 Vnet,并使用一个列表来存储子网的名称,然后是下面的示例核心代码:

locals {
  subnets = [for sname in var.subnet_names: {
    name = sname
    # the index begin from 0, so you need to add 1
    number = index(var.subnet_names, sname) + 1
  }]
}

resource "azurerm_virtual_network" "virtual_network" {
  address_space = "${var.cidr_network_range}"
  location = "${var.location}"
  name = "${var.virtual_network_resource_name}"
  resource_group_name = "${var.resource_group_name}"


  dynamic "subnet"{
    for_each = [for s in local.subnets: {
      name = s.name
      prefix = cidrsubnet(var.cidr_network_range, 8 , s.number)
    }]

    content {
      name = subnet.value.name
      address_prefix = subnet.value.prefix
    }
  }
}
于 2020-02-03T07:28:15.757 回答