附加现有键值对的地形代码
terraform code to append existing key value pair
我想使用 terraform 将新的键值对附加到现有的基于 yaml 的结构中。
例如我有以下 yaml 文件:
urls:
- id: site1
url: google.de
- id: site2
url: bing.com
- id: site3
url: duckduckgo.com
现在我想根据某些条件添加键值对。
(不需要写入文件,后面循环使用list)
预期:
urls:
- id: site1
url: google.de
secure: false
- id: site2
url: bing.com
secure: false
- id: site3
url: duckduckgo.com
secure: true
我已经尝试过的:
locals {
x = tomap({
for k in keys(yamldecode(file("urls.yaml"))):
k => merge(
yamldecode(file("urls.yaml"))[k],
{ urls = { url = merge(yamldecode(file("urls.yaml"))[k].urls[0], { secure = false }) }}
)
})
}
适用于第一个 url,但我无法遍历 url 来获取索引。
第二种方法:
locals {
x = tomap({
for k in keys(yamldecode(file("urls.yaml"))):
k => {
for l in keys(yamldecode(file("urls.yaml"))[k]):
l => l == "urls" ? <tbd> : yamldecode(file("urls.yaml"))[k][l]
}
})
}
但如果键匹配,我无法合并或替换 处的结构。
它总是因为不匹配而失败:
arguments must be maps or objects, got "tuple".
有什么想法吗?
yamldecode
函数将 YAMl 格式的字符串转换为 HCL2 后,生成的类型将是 map(list(object))
;例如:
{ "urls" = [{id="site1", url="google.de"}] }
这使得如何使用 for
表达式将 key-value 对添加到嵌套的 object
变得更加清晰。我们需要保留原始的结构、键和值,并在嵌套对象中添加单个键值对。
# map constructor and iterate through yaml map
# key = "urls", urls is list of objects
{ for key, urls in yamldecode(file("urls.yaml")) : key => [
# inside list constructor now
# url_attrs is single object in list of objects
for url_attrs in urls : {
# inside object constructor now
# retain same key value pairs, and add a "secure" key value pair
id = url_attrs.id
url = url_attrs.url
secure = false
}
]
}
在 HCL2 中,这导致(根据本地测试)
{
urls = [
{
id = "site1"
secure = false
url = "google.de"
},
]
}
相当于:
urls:
- id: site1
url: google.de
secure: false
我注意到 secure
布尔值的逻辑是一个占位符,示例代码总是分配 false
,所以我在上面做了同样的事情。
我想使用 terraform 将新的键值对附加到现有的基于 yaml 的结构中。
例如我有以下 yaml 文件:
urls:
- id: site1
url: google.de
- id: site2
url: bing.com
- id: site3
url: duckduckgo.com
现在我想根据某些条件添加键值对。
(不需要写入文件,后面循环使用list)
预期:
urls:
- id: site1
url: google.de
secure: false
- id: site2
url: bing.com
secure: false
- id: site3
url: duckduckgo.com
secure: true
我已经尝试过的:
locals {
x = tomap({
for k in keys(yamldecode(file("urls.yaml"))):
k => merge(
yamldecode(file("urls.yaml"))[k],
{ urls = { url = merge(yamldecode(file("urls.yaml"))[k].urls[0], { secure = false }) }}
)
})
}
适用于第一个 url,但我无法遍历 url 来获取索引。
第二种方法:
locals {
x = tomap({
for k in keys(yamldecode(file("urls.yaml"))):
k => {
for l in keys(yamldecode(file("urls.yaml"))[k]):
l => l == "urls" ? <tbd> : yamldecode(file("urls.yaml"))[k][l]
}
})
}
但如果键匹配,我无法合并或替换
它总是因为不匹配而失败:
arguments must be maps or objects, got "tuple".
有什么想法吗?
yamldecode
函数将 YAMl 格式的字符串转换为 HCL2 后,生成的类型将是 map(list(object))
;例如:
{ "urls" = [{id="site1", url="google.de"}] }
这使得如何使用 for
表达式将 key-value 对添加到嵌套的 object
变得更加清晰。我们需要保留原始的结构、键和值,并在嵌套对象中添加单个键值对。
# map constructor and iterate through yaml map
# key = "urls", urls is list of objects
{ for key, urls in yamldecode(file("urls.yaml")) : key => [
# inside list constructor now
# url_attrs is single object in list of objects
for url_attrs in urls : {
# inside object constructor now
# retain same key value pairs, and add a "secure" key value pair
id = url_attrs.id
url = url_attrs.url
secure = false
}
]
}
在 HCL2 中,这导致(根据本地测试)
{
urls = [
{
id = "site1"
secure = false
url = "google.de"
},
]
}
相当于:
urls:
- id: site1
url: google.de
secure: false
我注意到 secure
布尔值的逻辑是一个占位符,示例代码总是分配 false
,所以我在上面做了同样的事情。