Terraform嵌套循环表达式

huangapple go评论46阅读模式
英文:

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&#39;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&#39;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>



huangapple
  • 本文由 发表于 2023年2月18日 15:37:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定