[英]Query through YAML and create custom local for for_each loop
Let me introduce you to our setup.让我向您介绍我们的设置。 We have a
/apps
folder that includes some yml files:我们有一个
/apps
文件夹,其中包含一些 yml 文件:
/apps
|___ testing-one.yml
|___ testing-two.yml
apps/testing-one.yml应用程序/测试-one.yml
name: testing one
hostnames:
- name: testing-a
external: true
internal: true
internal_stages: ["dev","qs"]
- name: testing-b
external: true
internal: true
internal_stages: ["dev","qs"]
or apps/testing-two.yml或应用程序/测试-two.yml
name: testing-two
hostnames:
- name: testing-x
external: true
internal: true
internal_stages: ["dev","qs"]
As you can see, some files have just one entry under hostnames
other have two entry.如您所见,一些文件在
hostnames
名下只有一个条目,而其他文件有两个条目。 For further customization of the List, we create a local variable, that holds the data of each file under apps/
folder.为了进一步自定义列表,我们创建了一个局部变量,它保存了
apps/
文件夹下每个文件的数据。
applications = [for filename in fileset(path.module, "apps/*.yml") : yamldecode(file(filename))]
The output of local.applications
looks as followed: local.applications 的
local.applications
如下所示:
+ name = [
+ {
+ hostnames = [
+ {
+ external = true
+ internal = true
+ internal_stages = [
+ "dev",
+ "qs",
]
+ name = "testing-a"
},
+ {
+ external = true
+ internal = true
+ internal_stages = [
+ "dev",
+ "qs",
]
+ name = "testing-b"
},
]
+ name = "testing one"
},
+ {
+ hostnames = [
+ {
+ external = true
+ internal = true
+ internal_stages = [
+ "dev",
+ "qs",
]
+ name = "testing-x"
},
]
+ name = "testing-two"
},
]
What do we need?我们需要什么?
For later resource creation, we need a map with the following value.对于以后的资源创建,我们需要一个具有以下值的 map。
name
entry under hostname, create a map where the key
is testing-a
(value of hostname.name) and the value
is testing one
(value of just name)name
条目,创建一个 map,其中key
是testing-a
(主机名.name 的值), value
是testing one
(只是名称的值) Something like this:像这样的东西:
[
{testing-a: testing one},
{testing-b: testing one},
{testing-x: testing-two}
]
Why do we need that type of Map?为什么我们需要这种类型的 Map?
Later we have to create a resource with for_each inside that is using the key and the value of the list.稍后我们必须创建一个内部带有 for_each 的资源,该资源使用列表的键和值。
resource "local_file" "foo" {
for_each = {for key, value in local.final_list : key => value}
content = each.key
filename = each.value
}
What can I try to resolve this?我可以尝试什么来解决这个问题?
The following local variable outputs your expected result I believe:我相信以下局部变量会输出您的预期结果:
output = flatten([for app in local.applications : [
for host in app.hostnames : {
(host.name) = app.name
}
]])
The output I received after creating an output called "out_test" is as follows:我在创建名为“out_test”的 output 后收到的 output 如下:
+ out_test = [
+ {
+ testing-a = "testing one"
},
+ {
+ testing-b = "testing one"
},
+ {
+ testing-x = "testing-two"
},
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.