在资源本身使用for_each的资源子元素中使用for循环进行插补

时间:2020-05-18 02:22:57

标签: terraform terraform0.12+

例如,这是一个与for_each一起部署的资源(非常简化)(for_each isnt不是Im遇到问题的地方,我可以整天这样做-它试图正确插入ovf_network_map中的数据就是Im遇到问题的地方) :

resource "vsphere_virtual_machine" "vmFromLocalOvf" {
  for_each = var.customers[var.customer][var.idc].vms
  ...snip...
  ovf_deploy {
    local_ovf_path = "cucm_11.5_vmv8_v1.1.ovf"        
    ovf_network_map = {
      for net in ["INSIDE", "OUTSIDE"]:
      "eth${count.index}" => data.vsphere_network.net.id
    }

对于此简化示例,最终目标是ovf_network_map包含{{eth0“ = data.vsphere_network.INSIDE.id,” eth1“ = data.vsphere_network.OUTSIDE.id} (显然,该数据对象将在此处进一步插值,但希望这里能解决Im试图完成的工作)。

有2个错误: The "count" object can be used only in "resource" and "data" blocks, and only when the "count" argument is set.A data resource "vsphere_network" "net" has not been declared,显然我的插值有误。希望我在这里需要的插值是可能的-我可能会以错误的方式进行操作-有任何想法吗?

编辑以添加:通过以下操作,我能够找出eth0和eth1的数字计数:eth${index(slice(var.customers[var.customer][var.idc].vms[each.key], 3, length(var.customers[var.customer][var.idc].vms[each.key]) - 1), net)}" => data.vsphere_network.net.id

现在剩下的全部-我遇到错误data.vsphere_network.net.id时,试图在A data resource "vsphere_network" "net" has not been declared的“网”中进行“双”插值

2 个答案:

答案 0 :(得分:1)

for_each 中,您不会获得 count 变量或相应的 count.index ,因此无法正常工作:< / p>

resource "vsphere_virtual_machine" "vmFromLocalOvf" {
  for_each = var.customers[var.customer][var.idc].vms
  ...snip...
  ovf_deploy {
    local_ovf_path = "cucm_11.5_vmv8_v1.1.ovf"        
    ovf_network_map = {
      for net in ["INSIDE", "OUTSIDE"]:
      "eth${count.index}" => data.vsphere_network.net.id
    }

可以从索引到值的映射如下,然后像使用 count.index 一样使用 each.key

resource "vsphere_virtual_machine" "vmFromLocalOvf" {
  for_each = zipmap(
    range(length(var.customers[var.customer][var.idc].vms)),
    var.customers[var.customer][var.idc].vms
  )
  ...snip...
  ovf_deploy {
    local_ovf_path = "cucm_11.5_vmv8_v1.1.ovf"        
    ovf_network_map = {
      for net in ["INSIDE", "OUTSIDE"]:
      "eth${each.key}" => data.vsphere_network.net.id
    }

答案 1 :(得分:0)

Alain在zipmap上有正确的想法。但是,我不得不将数据抽象到locals {}变量中,而不是双重插值(不支持该插值),而这就是zipmap所在的位置:

data "vsphere_network" "networks" {
  count                           = length(var.customers[var.customer][var.idc].networks)
  name                            = "CUST-${substr(var.customer, 5, length(var.customer) - 4)}-UC-${var.customers[var.customer][var.idc].networks[count.index]}"
  datacenter_id                   = data.vsphere_datacenter.dc.id
  distributed_virtual_switch_uuid = data.vsphere_distributed_virtual_switch.dvs.id
}
locals {
  netids = zipmap(var.customers[var.customer][var.idc].networks, data.vsphere_network.networks.*.id)
}

resource "vsphere_virtual_machine" "vmFromLocalOvf" {
  for_each = var.customers[var.customer][var.idc].vms
  name     = "${substr(var.customer, 0, 4)}-${each.key}"
  ovf_deploy {
    local_ovf_path = "ov/${var.customers[var.customer][var.idc].vms[each.key].1}"
    ovf_network_map = {
      for net in slice(var.customers[var.customer][var.idc].vms[each.key], 3, length(var.customers[var.customer][var.idc].vms[each.key]) - 1) :
      "eth${index(slice(var.customers[var.customer][var.idc].vms[each.key], 3, length(var.customers[var.customer][var.idc].vms[each.key]) - 1), net)}" => local.netids[net]
    }
  }
  vapp {
    properties = {
      "DeploymentOption.value" = var.customers[var.customer][var.idc].vms[each.key].2
    }
  }
}