英文:
Terraform nested loop expression
问题
I'm trying to construct a collection to loop over based on nested data. The example below shows the code I am trying to use:
repos.tfvars
repositories = { workflows = { description = "Reusable workflow repository" topics = ["github-actions", "reusable-workflows"] }, aws-network-speciality = { description = "AWS Network Speciality study repository" topics = ["amazon-web-services", "aws", "terraform", "iac"] } }
main.tf
resource "github_repository_file" "tflint" { for_each = local.terraform_repos repository = each.key file = ".tflint" content = file("${path.module}/github_repository_files/.tflint") commit_message = "chore(managed-by-terraform): tflint configuration file" } ```
The desired outcome is that tflint is uploaded to each repository with the terraform topic.
Currently `local.terraform_repos` returns an empty map - I'm just unsure how to drill down into `var.repositories` to query the `topics[]` and then return the name of the repo(s) for the loop.
<details>
<summary>英文:</summary>
I'm trying to construct a collection to loop over based on nested data. The example below shows the code I am trying to use:
repos.tfvars
repositories = {
workflows = {
description = "Reusable workflow repository"
topics = ["github-actions", "reusable-workflows"]
},
aws-network-speciality = {
description = "AWS Network Speciality study repository"
topics = ["amazon-web-services", "aws", "terraform", "iac"]
}
}
main.tf
locals {
terraform_repos = { for each, repo in var.repositories : each => repo if contains(["terraform"], repo)}
resource "github_repository_file" "tflint" {
for_each = local.terraform_repos
repository = each.key
file = ".tflint"
content = file("${path.module}/github_repository_files/.tflint")
commit_message = "chore(managed-by-terraform): tflint configuration file"
}
The desired outcome is that tflint is uploaded to each repository with the terraform topic.
Currently `local.terraform_repos` returns an empty map - I'm just unsure how to drill down into `var.repositories` to query the `topics[]` and then return the name of the repo(s) for the loop.
</details>
# 答案1
**得分**: 2
`contains` 应该用于 `topics`,而且您的参数有误。它应该如下所示:
terraform_repos = { for each, repo in var.repositories : each => repo if contains(repo.topics, "terraform")}
<details>
<summary>英文:</summary>
`contains` should be used for `topics` and also you have wrong arguments. It should be:
terraform_repos = { for each, repo in var.repositories : each => repo if contains(repo.topics, "terraform")}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论