简体   繁体   English

通过 YAML 查询并为 for_each 循环创建自定义本地

[英]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。

  1. For every 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,其中keytesting-a (主机名.name 的值), valuetesting 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.

相关问题 Terraform - 如何在对象列表上使用 for_each 循环来创建资源 - Terraform - how to use for_each loop on a list of objects to create resources 如何跳过在根模块中声明值(for_each 循环) - How to skip declaring values in root module (for_each loop) 如何遍历每个列表并在其中添加本地存储项 - how to loop through each list and add local stored item in it 每次通过bash for循环创建一个新文件 - Create a new file each time through a bash for loop jquery / javascript-如何遍历数组并在每次迭代中创建变量? - jquery/javascript -how to loop through array and create variables in each iteration? XSLT:for_each位于两个列表中 - XSLT: for_each on two lists 对于每个循环不遍历一切 - For each loop not looping through everything 循环打印两个列表,以获得两列在每个列表的每个元素的第一个字母之间具有固定(自定义集)空间 - Loop print through two lists to get two columns with fixed(custom set) space between the first letter of each element of each list for_each和mem_fun_ref麻烦 - for_each and mem_fun_ref trouble 如何在R数据帧中的列之间循环并在每次迭代中使用列名创建新的数据帧? - How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM