Terraform:来自 yaml 的嵌套 for 循环
Terraform: nested for loop from yaml
我正在尝试 运行 在 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"))
}
我想 运行 使用 for_each 进入“Dev”和“Ops”列表中的对象列表。
例如,我想在“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"
}
]
等等
如何在 Terraform 上执行此操作?
如果我没理解错的话,您要提取的只是那个 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
}
Terraform 计划如下所示:
Changes to Outputs:
+ test = [
+ {
+ email = "danielf@example.com"
+ firstname = "daniel"
+ lastname = "folsik"
},
+ {
+ email = "johnb@example.com"
+ firstname = "john"
+ lastname = "belk"
},
]
该格式应该比您建议的键值更容易在最终资源中循环
我正在尝试 运行 在 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"))
}
我想 运行 使用 for_each 进入“Dev”和“Ops”列表中的对象列表。
例如,我想在“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"
}
]
等等
如何在 Terraform 上执行此操作?
如果我没理解错的话,您要提取的只是那个 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
}
Terraform 计划如下所示:
Changes to Outputs:
+ test = [
+ {
+ email = "danielf@example.com"
+ firstname = "daniel"
+ lastname = "folsik"
},
+ {
+ email = "johnb@example.com"
+ firstname = "john"
+ lastname = "belk"
},
]
该格式应该比您建议的键值更容易在最终资源中循环