我正在尝试在terraform上运行嵌套的for
循环
我有以下Yaml文件:
Employees:
- Department:
- Dev:
- name: "danielf"
role: developer
details:
email : danielf@example.com
firstname : daniel
lastname : folsik
- name: "johnb"
role: developer
details:
email : johnb@example.com
firstname : john
lastname : belk
- Ops:
- name: "benol"
role: devops
details:
email : benol@example.com
firstname : ben
lastname : olkin
- name: "pauld"
role: devops
details:
email : pauld@example.com
firstname : paul
lastname : dempler
我正在使用locals
获取yaml数据:
locals {
ou_config = yamldecode(file("employees.yaml"))
}
我想在";Dev";以及";Ops";使用for_each列出
例如,我想在"Dev";列表以在第一次迭代中获得以下对象列表:
[
{
key = "email"
value = "danielf@example.com"
},
{
key = "firstname"
value = "daniel"
},
{
key = "lastname"
value = "folskin"
}
]
for_each上的下一次运行将是:
[
{
key = "email"
value = "johnb@example.com"
},
{
key = "firstname"
value = "john"
},
{
key = "lastname"
value = "belk"
}
]
等等
如何在地形上进行
如果我理解正确,您试图提取的只是该yaml文件的details
部分。。。
以下是我要做的一切:
locals {
ou_config = yamldecode(file("employees.yaml"))
expanded_names = flatten([
for e in local.ou_config.Employees : [
for d in e.Department : [
for key, person in d : [
for key, value in person : [
value.details
]
]
]
]
])
}
output "test" {
value = local.expanded_names
}
如果我们想过滤,我们添加一个if key == "Dev"
locals {
ou_config = yamldecode(file("employees.yaml"))
expanded_names = flatten([
for e in local.ou_config.Employees : [
for d in e.Department : [
for key, person in d : [
for key, value in person : [
value.details
]
] if key == "Dev"
]
]
])
}
output "test" {
value = local.expanded_names
}
上面的地形图看起来像:
Changes to Outputs:
+ test = [
+ {
+ email = "danielf@example.com"
+ firstname = "daniel"
+ lastname = "folsik"
},
+ {
+ email = "johnb@example.com"
+ firstname = "john"
+ lastname = "belk"
},
]
该格式应该比您建议的键值更容易在最终资源中循环