azure 蔚蓝色nics上的地形foreach(Map)

kmb7vmvb  于 2022-11-30  发布在  其他
关注(0)|答案(1)|浏览(122)

I'm trying to convert those variables in main.tf to terraform.tfvars and variables.tf ( declarer the variables in terraform.tfvars and variables.tf ).
我希望在创建新VM时使用几个网络接口名称和虚拟机名称并在它们上循环来创建Map(Map是必需,因为我对每个都使用)。

    • 主文件. tf**

here i create the variables in the main.tf but as i wrote i want to declarer them in the variables.tf file

variable "nics" {
  type = map(any)
  default = {

    nic3 = {
      name = "ubuntutest3"
    }

    nic4 = {
      name = "ubuntutest4"
    }
  }
}

variable "vms" {
  description = "Virtual Machines"
  type        = map(any)
  default = {
    vm3 = {
      name = "ubuntutest3"
      size = "Standard_DS1_v2"
      nic  = "nic3"
    }
    vm4 = {
      name = "ubuntutest4"
      size = "Standard_DS1_v2"
      nic  = "nic4"
    }
  }
}

// VNICs
resource "azurerm_network_interface" "nics" {
  for_each            = var.nics
  name                = each.value.name
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "${each.value.name}-conf"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

当我创建虚拟机时,

network_interface_ids            = [azurerm_network_interface.nics[each.value.nic].id, ]

when i tried to declarer those variables in variable.tf file and terraform.tfvars i got error massage

│ Error: Invalid index
│
│   on main.tf line 259, in resource "azurerm_virtual_machine" "vms":
│  259:   network_interface_ids            = [azurerm_network_interface.nics[each.value.nic].id, ]
│     ├────────────────
│     │ azurerm_network_interface.nics is object with 2 attributes
│     │ each.value.nic is "network_interface_2"
│
│ The given key does not identify an element in this collection value.

how should i declarer on vm names and nic names in variables.tf and create the names i want to use in terraform.tfvars and use them I'm main.tf ?

    • 已编辑**

我试过这样

    • 地形变化**
nics = {
  nic_names = ["nic2,nic3"]
}
    • 变量. tf**
variable "nics" {
  type = object({
    nic_names = list(string)
  })
}

得到错误

│ Error: Unsupported attribute
│
│   on main.tf line 272, in resource "azurerm_network_interface" "nics":
│  272:   name                = each.value.nic_names
│     ├────────────────
│     │ each.value is list of string with 1 element
│
│ This value does not have any attributes.
ctehm74n

ctehm74n1#

您的错误与使用的tfvar而不是变量无关。错误明确指出,当您创建VM时,each.value.nic在此特定迭代中是network_interface_2,而在nicsMap中您只有键nic3nic4
至于第二部分,您可能希望键入:

nic_names = ["nic2","nic3"]

而不是:

nic_names = ["nic2,nic3"]

第二种是只有一个字符串的列表,第一种是两个元素的列表。

    • 编辑**

要直接演示@ITBYD如何将其文件设置为工作解决方案:
terraform.tfvars:

nics = {
  nic_names = ["nic2","nic3"]
}

main.tfvars:

variable "nics" {
  type = object({
    nic_names = list(string)
  })
}

# [...]

resource "azurerm_network_interface" "nics" {
  for_each            = toset(var.nics["nic_names"])
  name                = each.value
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "${each.value}-conf"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

但我不会使用列表,只使用Map,就像我之前写的那样。

相关问题