简体   繁体   English

在模块中使用 for_each 引用属性并在资源中计数

[英]Referencing properties with for_each in modules and count in the resource

Here is my shortened code:这是我的缩短代码:

variable.tf : variable.tf

variable "vm_avd" {
  type = map(any)
  default = {
    avd-std = {
      vm_count = "2"
      ### Interface Vars
      subnet_id = "AD"
    }
    avd-gpu = {
      vm_count = "2"
      ### Interface Vars
      subnet_id = "AD"
    }
  }
}

variable "vm_server" {
  type = map(any)
  default = {
    srv-std = {
      vm_count = "2"
      ### Interface Vars
      subnet_id = "AD"
    }
  }
}

if.tf : if.tf

resource "azurerm_network_interface" "if" {
  count               = var.vm_count
  name                = var.private_ip_address_allocation == "Static" ? "if_${var.if_name}_${replace(var.private_ip_address, ".", "")}_${format("%02d", count.index)}" : "if_${var.if_name}_${format("%02d", count.index)}"
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "if_ipconfig"
    subnet_id                     = var.subnet_id
    private_ip_address_version    = var.private_ip_address_version
    private_ip_address_allocation = var.private_ip_address_allocation
    private_ip_address            = var.private_ip_address
  }
}
output "if_name" {
  value = azurerm_network_interface.if[*].name
}
output "if_id" {
  value = azurerm_network_interface.if[*].id
}

main.tf : main.tf

module "vm" {
  for_each              = merge(var.vm_server, var.vm_avd)
  vm_name               = each.key
  vm_count              = each.value["vm_count"]
  network_interface_ids = module.if[each.key].if_id
}

module "if" {
  for_each  = merge(var.vm_server, var.vm_avd)
  vm_count  = each.value["vm_count"]
  if_name   = each.key
  subnet_id = module.snet[each.value["subnet_id"]].snet_id
}

Is there a possibility to reference in a for_each loop the correct count object for dependent resources?是否有可能在for_each循环中引用依赖资源的正确count object?

I'm creating Terraform modules for Azure and I want to build a structure that is highly flexible and easy to fill for everyone.我正在为 Azure 创建 Terraform 模块,我想构建一个高度灵活且易于填充的结构。

Therefore I build a root file with a single module call for each resource and I have a single main variables file which should be filled with values which are looped through.因此,我为每个资源构建了一个带有单个模块调用的根文件,并且我有一个主变量文件,该文件应填充循环遍历的值。

All related resource variables are defined in the same variables object, so for a VM a variable VM is created and filled with VM details, NIC details and extension details.所有相关的资源变量都在相同的变量 object 中定义,因此对于 VM,将创建一个变量 VM 并填充 VM 详细信息、NIC 详细信息和扩展详细信息。 The single modules run through the same variables for VM, NIC and extensions which is, in general, running fine.单个模块通过 VM、NIC 和扩展的相同变量运行,通常运行良好。

I can reference the su.net_id for the.network interfaces nicely but as soon as I use a count operator in the resource I don't know how the correctly get the.network interface ID of the correct interface for the correct VM.我可以很好地引用 .network 接口的su.net_id ,但是一旦我在资源中使用count运算符,我就不知道如何正确获取正确 VM 的正确接口的 .network 接口 ID。

Now I ran into the problem that I use for_each in my module call and I want to have the option to use count in my resource definition too.现在我遇到了在我的模块调用中使用 for_each 的问题,我也想在我的资源定义中选择使用 count。 It is working but when I build eg virtual machines, I get the problem that I cannot reference to the correct.network interfaces.它正在工作,但是当我构建例如虚拟机时,我遇到了无法引用正确的网络接口的问题。 Atm all my interfaces are connected to the same VM and the 2nd VM cannot be build. Atm 我的所有接口都连接到同一个虚拟机,无法构建第二个虚拟机。 Same goes for extensions and any possible multiple objects tho.扩展和任何可能的多个对象也是如此。

I tried several module calls but the first VM got all the interfaces every time.我尝试了几个模块调用,但第一个 VM 每次都获得了所有接口。 I tried with the Star character in the NIC module call or with the.network interface ID.我尝试在 NIC 模块调用中使用 Star 字符或使用 .network 接口 ID。 If this is not possible at all, I thought of a solution with building the.network interface ID itself with its original form.如果这根本不可能,我想到了一个解决方案,即使用其原始形式构建 .network 接口 ID 本身。

I also checked if there is a possibility to build an array with the for_each and count elements but I couldn't find any way to build arrays out of number variables in terraform, like the normal "for 1 to 5 do".我还检查了是否有可能用for_each和 count 元素构建一个数组,但我找不到任何方法来构建 arrays terraform 中的数字变量,就像正常的“for 1 to 5 do”一样。 If you know of any way to do this, I would be grateful too.如果您知道任何方法可以做到这一点,我也将不胜感激。

Maybe it is a typo when you try to simplify the question, but in your variables you have:当您尝试简化问题时,这可能是一个错字,但在您的变量中您有:

variable vm_avd { default = { avd-std = {...}} }
variable vm_server { default = { avd-std = {...}} }

in your main tf you have:在您的主要 tf 中,您有:

merge(var.vm_server, var.vm_avd)

since the key is the same, one will override the other, if you are using the default value, it might result in the behaviour you are suggesting, consider changing the key name to server-std?由于密钥相同,一个将覆盖另一个,如果您使用默认值,则可能会导致您建议的行为,考虑将密钥名称更改为 server-std?

I got the solution with simply providing the whole element array, eg from VM, to the variable for the ID and the reference the correct VM with the count index:我通过简单地将整个元素数组(例如从 VM)提供给 ID 的变量并使用计数索引引用正确的 VM 来获得解决方案:

virtual_machine_id = var.vm_id[count.index] virtual_machine_id = var.vm_id[count.index]

As i run through the same Count in other ressources, I get the correct order of the elements for referencing:)当我在其他资源中运行相同的计数时,我得到了引用元素的正确顺序:)

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

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