1

试图将一些东西放在一起以通过 tfe 插件的限制。

我有 200 多个工作区,我使用 Terraform Cloud 中的一个需要更新的变量进行管理。在这种情况下,我需要更新的所有工作空间都以“dev-workspace”开头。

我有一个包含以下内容的数据块:

data "tfe_workspace_ids" "all" {
  names        = ["*"]
  organization = "myorganization"
}

由于模块的限制,我无法对这些工作区进行通配符搜索。此数据块返回包含我所有工作区的字符串映射:

aa = {
    "dev-workspace-1"                   = "ws-anonymized"
    "dev-workspace-2"                   = "ws-ws-anonymized"
    "dev-workspace-3"                   = "ws-ws-anonymized"
    "test-workspace-1"                  = "ws-ws-anonymized"
    "prod-workspace-1"                  = "ws-ws-anonymized"
}

我的问题是我需要获取此字符串映射并将其过滤以仅返回键中具有“dev-workspace”的字符串。我尝试过类似以下的方法:

resource "tfe_variable" "dev-workspace" {
  for_each = contains(data.tfe_workspace_ids.all.ids, "dev-workspace")

  key = "access_key"
  value = "XXXX"
  category = "terraform"
  workspace_id = each.value
  sensitive = true
  description = "AWS IAM secret access key."
}

但看起来你不能contains以这种方式使用for_each

Error: Error in function call

  on main.tf line 16, in resource "tfe_variable" "dev-workspace":
  16:   for_each = contains(data.tfe_workspace_ids.all.ids, "dev-workspace")
    |----------------
    | data.tfe_workspace_ids.all.ids is map of string with 284 elements

Call to function "contains" failed: argument must be list, tuple, or set.

我不太确定在这里做什么,但是已经尝试了几种方法并且无法弄清楚。谢谢你的帮助。

4

1 回答 1

2

如果要过滤,您的资源可能是(您必须更改var.aadata.tfe_workspace_ids生成输入映射的值):


variable "aa" {

  default = {
      "dev-workspace-1"                   = "ws-anonymized"
      "dev-workspace-2"                   = "ws-ws-anonymized"
      "dev-workspace-3"                   = "ws-ws-anonymized"
      "test-workspace-1"                  = "ws-ws-anonymized"
      "prod-workspace-1"                  = "ws-ws-anonymized"
  }
}

resource "tfe_variable" "dev-workspace" {

  for_each = {for k, v in var.aa:
              k => v if length(regexall("dev-workspace", k)) > 0}

  key = "access_key"
  value = "XXXX"
  category = "terraform"
  workspace_id = each.value
  sensitive = true
  description = "AWS IAM secret access key."
}
于 2021-02-23T23:41:41.617 回答