0

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

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 } (显然,数据对象将被进一步插值那里,但希望我试图完成的问题在这里出现)。

有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

4

2 回答 2

1

您不会在for_each中获得count变量或相应的count.index,因此这是行不通的:

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
    }
于 2020-05-18T02:45:28.400 回答
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
    }
  }
}

于 2020-05-19T02:03:43.657 回答