![](/img/trans.png)
[英]Iterating through map of objects in terraform with route 53 records
[英]Iterating network interfaces in vsphere provider with Terraform
問題:如何遍歷嵌套的 map 來為數據資源塊分配字符串值?
上下文:在 terraform 上處理使用 vsphere 提供程序 2.0 通過 OVA 模板部署多個 VM 的要求。
由於 .network 接口會因環境而異,因此 OVA 模板將僅包含任何環境中所有 VM 通用的“全局”.network 接口。
我正在使用 vsphere.network 數據資源來檢索分配給虛擬機的每個網絡接口的分布式虛擬端口組 ID。
目前停留在變量插值上以迭代此信息以分配給 terraform 中的每個 vm 資源。
1 個 vsphere.network 數據塊用於迭代所有 DVPG id,1 個 vm 資源塊用於使用 dynamic.network 接口塊部署所有具有這些 DVPG 的虛擬機
虛擬機配置變量:
variable "vmconfig" {
description = "Map of VM name => Configs "
type = map(object({
name = string
cpus = number
memory = number
folder = string
remote_ovf = string
netint = map(string)
}))
default = {}
}
.tfvars:
vmconfig = {
"vm1" = {
name = "vm1"
cpus = 4
memory = 16384
folder = "foo/bary"
remote_ovf = "foo.bar.ova"
netint = {
nic1 = "segment1",
nic2 = "segment2",
nic3 = "segment3",
nic4 = "segment4"
}
},
"vm2" = {...}, etc.
將上面的變量調用到局部變量中:
locals {
vm_values = { for name, config in var.vmconfig : name => {
vm_name = config.name
num_cpus = config.cpus
memory = config.memory
folder = config.folder
remote_ovf_url = config.remote_ovf
netint = config.netint
}
}
}
嘗試使用 for_each 而不是 count 遍歷數據資源塊內的每個 for.netint 值(列為正在部署的 vm 類型的最佳實踐):
data "vsphere_network" "nicint" {
for_each = local.vm_values
name = each.value.netint
datacenter_id = data.vsphere_datacenter.dc.id
}
然后使用動態在 VM 資源塊內部調用此數據資源塊:
resource "vsphere_virtual_machine" "vm" {
.
.
.
dynamic "network_interface" {
for_each = data.vsphere_network.nicint
content {
network_id = network_interface.value.id
}
}
}
我遇到的問題是遍歷 inside.netint 中的每個值,我得到的暗示是我可能在這里遺漏了一些微不足道的東西,非常感謝您支持准確定義 for_each 迭代,以便可以使用編程方式使用多個 vsphere.network 數據源那一個數據塊。
我嘗試了以下變體來迭代數據塊:
data "vsphere_network" "nicint" {
for_each = {for k,v in local.vm_values : k => v.netint}
name = each.value
datacenter_id = data.vsphere_datacenter.dc.id
}
我得到的錯誤是:屬性“名稱”的值不合適:需要字符串 each.value 是一個 map 的字符串,包含 4 個元素
我嘗試使用合並,它有效,但它最終為每個虛擬機創建了副本,並且不會修改現有資源。 但摧毀並創造另一個。
另一個局部變量創建到 map the.network 接口段:
netint_map = merge([
for vmtype, values in var.vmconfig:
{
for netint in values.netint:
"${vmtype}-${netint}" => {vmtype = vmtype, netint = netint}
}
]...)
data "vsphere_network" "nicint" {
for_each = local.netint_map
name = each.value
datacenter_id = data.vsphere_datacenter.dc.id
}
親愛的 Hivemind,請指導我有效地優化它 - 謝謝!
您的合並是正確的。 所以我只是在這里發布以供參考:
locals {
netint_map = merge([
for vmtype, values in var.vmconfig:
{
for netint in values.netint:
"${vmtype}-${netint}" => {vmtype = vmtype, netint = netint}
}
]...)
}
data "vsphere_network" "nicint" {
for_each = local.netint_map
name = each.value
datacenter_id = data.vsphere_datacenter.dc.id
}
我認為問題出在您的動態塊上。 也就是說,而不是for_each = data.vsphere.network.nicint
你應該從你的變量而不是數據源迭代nicint
。
resource "vsphere_virtual_machine" "vm" {
for_each = var.vmconfig
#...
dynamic "network_interface" {
for_each = toset(each.value.netint)
content {
network_id = data.vsphere_network.nicint["${each.key}-${network_interface.key}"].id
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.