列表的地形插值?

时间:2018-09-20 16:17:10

标签: terraform terraform-provider-aws

我正在尝试使用terraform gitlab-provider。我想以列表的形式传递地图,并在模块中调用它们。

我该如何实现?在这里可以使用任何插值语法吗?

# names and can_create_groups
variable "names" {
  type    = "list"
  default = [
    {
     "name"             = "test"
     "username"         = "test"
     "email"            = "test@xxxxx.com"
     "project_limit"    = "100"
     "can_create_groups"= "false"
     "is_admin"         = "false"
    },
    {
     "name"             = "test2"
     "username"         = "tetst.2"
     "email"            = "test2@xxxx.com"
     "project_limit"    = "100"
     "can_create_groups"= "true"
     "is_admin"         = "false"
    }
  ]
}

GIT-用户

resource "gitlab_user" "user" {
  name             = "${element(var.names,count.index)}"
  username         = "${element(var.names,count.index)}"
  password         = "dummypassword"
  email            = "${element(var.names,count.index)}"
  is_admin         = "${element(var.names,count.index)}"
  projects_limit   = "${element(var.names,count.index)}"
  can_create_group = "${element(var.names,count.index)}"
  count            = 2
}

错误: 错误:gitlab_user.user:发生2个错误:

  • gitlab_user.user [0]:element:element()仅可用于平面列表,此列表包含以下类型的map元素:

$ {element(var.names,count.index)} * gitlab_user.user [1]:element:element()仅可用于平面列表,此列表包含以下类型的map元素:

$ {element(var.names,count.index)}

2 个答案:

答案 0 :(得分:1)

您可以使用一些built in Terraform functions使其正常工作

resource "gitlab_user" "user" {
  name             = "${lookup(var.names, element(keys(var.names), count.index))}"
  password         = "dummypassword"

  etc, etc, etc

  count            =  "${length(keys(var.names))}"
}

答案 1 :(得分:0)

resource "gitlab_user" "user" {
  name             = "${lookup(var.gitlab_users[count.index], "name")}"
  username         = "${lookup(var.gitlab_users[count.index], "username")}"
  password         = "dummypassword"
  email            = "${lookup(var.gitlab_users[count.index], "email")}"
  is_admin         = "${lookup(var.gitlab_users[count.index], "is_admin")}"
  projects_limit   = "${lookup(var.gitlab_users[count.index], "projects_limit")}"
  can_create_group = "${lookup(var.gitlab_users[count.index], "can_create_groups")}"
  count            = "${length(var.gitlab_users)}"
}