我需要生成大約 50 個 YAML 檔案。YAML 中的大部分內容是不變的,只有name
,并且是port
變化的。我正在使用Terraform v1.2.2
. 我請求您幫助我糾正我的問題或提出替代想法/解決方案以實作我的目標
netwrokfile.yaml
一代之后
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: web-policy # Changes
spec:
action: allow
rules:
- to:
- operation:
ports: ["9080", "8080"] # Changes from each application may have one or two or three ports.
selector:
matchLabels:
app: web-policy # Changes
networkfile.yaml.tpl
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
%{~ for sName, sPort in zipmap(sName, sPort) ~}
name: ${sName}
spec:
action: allow
rules:
- to:
- operation:
ports: ${sPort}
selector:
matchLabels:
app: ${sName}
%{~ endfor ~}
main.tf
在我的模塊中
resource "kubernetes_manifest" "istio-config" {
manifest = templatefile("${path.module}/templates/networkfile.yaml.tpl",
{
sName = [for v in var.serviceName: v.sName]
sPort = [for v in var.serviceName: v.sPort]
}
)
}
main.tf
呼叫我的模塊
module "authorization_policy" {
source = "../../modules/GlobalNetworkPolicy"
serviceName = var.serviceName
}
我的variable.tf
variable "serviceName" {
type = map(object({
sName = string
sPort = list(string)
}))
default = {
test-vm1 = {
sName = "web-policy"
sPort = ["8080", "8081"]
},
test-vm2 = {
sName = "db-policy"
sPort = ["8080", "8081", "443"]
}
}
}
使用上面的代碼,我得到以下錯誤
can't unmarshal tftypes.String into *map[string]tftypes.Value, expected map[string]tftypes.Value
我嘗試使用此處提到的解決方案(Terraform - Iterate over a List of Objects in a Template)。但它沒有幫助,它給了我同樣的錯誤。
我也試過(https://www.terraform.io/language/functions/templatefile)它給了我一個完全不同的錯誤
Call to function "templatefile" failed: ../../modules/GlobalNetworkPolicy/templates/networkfile.yaml.tpl:5,3-4: Unsupported operator; Bitwise operators are not supported. Did you mean boolean NOT ("!")?,
│ and 4 other diagnostic(s).
以下是完整的錯誤
?
│ Error: Failed to extract "manifest" attribute value from resource configuration
│
│ with module.authorization_policy.kubernetes_manifest.istio-config,
│ on ../../modules/GlobalNetworkPolicy/main.tf line 2, in resource "kubernetes_manifest" "istio-config":
│ 2: manifest = templatefile("${path.module}/templates/networkfile.yaml.tpl",
│ 3: {
│ 4: serviceName = var.serviceName
│ 5: }
│ 6: )
│
│ can't unmarshal tftypes.String into *map[string]tftypes.Value, expected map[string]tftypes.Value
uj5u.com熱心網友回復:
您可以將您的代碼簡單地寫為:
resource "kubernetes_manifest" "istio-config" {
manifest = yamldecode(templatefile("${path.module}/templates/networkfile.yaml.tpl",
{
serviceName = var.serviceName
}
))
}
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
%{ for k, v in serviceName }
name: ${v.sName}
spec:
action: allow
rules:
- to:
- operation:
ports: ${jsonencode(v.sPort)}
selector:
matchLabels:
app: ${v.sName}
%{ endfor }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/490460.html
標籤:亚马逊网络服务 地形 terraform-provider-aws terraform0.12 terraform-模板文件