在terraform中,如果设置了特定变量,是否只能添加可选的资源参数

时间:2018-11-21 23:50:06

标签: terraform rancher

在我的具体示例中:

我想用预先配置的成员创建一个牧场者环境资源。但是,成员的数量应该取决于变量列表。我会想像

resource "rancher_environment" "renv" {
  name = "renv"
  project_template_id = "atmplid"

  member {
    count = "${length(var.memberlist)}"
    external_id = "${var.memberlist[count.index]}"
    external_id_type = "exttype"
    role = "owner"
  }
}

这显然行不通。有没有技巧可以实现这种行为?

2 个答案:

答案 0 :(得分:2)

您可以为此使用null_resource。试试这个

resource "null_resource" "memberlist" {
    count = "${length(var.memberlist)}"
    triggers {
        external_id = "${var.memberlist[count.index]}"
        external_id_type = "exttype"
        role = "owner"
    }
}

resource "rancher_environment" "renv" {
    name = "renv"
    project_template_id = "atmplid"

    member = ["${null_resource.memberlist.*.triggers}"]
}

答案 1 :(得分:1)

终于,Terraform刚刚发布了v0.12.0-alpha1,其中包含a more elegant way of solving this exact problem