如何在Terraform的helm_release中传递对象列表类型变量
问题解决:Terraform模板渲染Tolerations为正确YAML格式
直接在模板中插入tolerations变量会输出Terraform内部的对象表示字符串,而非标准YAML结构。要解决这个问题,需要利用Terraform的yamlencode和indent函数将变量转换成合法的YAML格式。
修改模板文件
将metabase-test.tpl内容修改为:
tolerations: ${indent(2, yamlencode(tolerations))}
原理说明
yamlencode:将Terraform的list(object)类型变量转换为标准的YAML数组字符串,自动处理每个对象的键值对格式。indent(2, ...):给转换后的YAML字符串每一行添加2个空格缩进,确保符合YAML的层级要求,和tolerations:保持正确的嵌套关系。
验证效果
当你传入包含两个元素的tolerations变量时,渲染后的YAML会和你预期的完全一致:
tolerations: - key: metabase1 operator: Equal value: "true" effect: NoSchedule - key: metabase2 operator: Equal value: "true" effect: NoSchedule
内容的提问来源于stack exchange,提问作者user430953
相关产品推荐
相关产品推荐

