首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terraform无效的"each“属性

Terraform无效的"each“属性
EN

Stack Overflow用户
提问于 2021-06-30 07:19:11
回答 1查看 27关注 0票数 1

我有一个tfvars.json文件,我想用它来传递我的服务器配置。

代码语言:javascript
复制
{
  "test": "test",
  "machines": {
    "master01": {
      "node_type": "master",
      "image": "ubuntu-20.04",
      "server_type": "cx21",
      "location": "nbg1"
    },
    "master02": {
        "node_type": "master",
        "image": "ubuntu-20.04",
        "server_type": "cx21",
        "location": "nbg1"
      }
  }
}

现在,在我的main.tf中,我想创建实例

代码语言:javascript
复制
############## Provider ################
terraform {
  required_providers {
    hcloud = {
      source = "hetznercloud/hcloud"
      version = "1.26.2"
    }
  }
}

############## Variables ###############

# machines variable
variable "machines" {}

# Token variable
variable "hcloud_token" {
  default = "<Secret Key>"
}

# Define Hetzner provider
provider "hcloud" {
  token = "${var.hcloud_token}"
}

# Obtain ssh key data
data "hcloud_ssh_key" "ssh_key" {
  fingerprint = "<Secret Fingerprint>"
}

# Create Master Server
resource "hcloud_server" "master" {
  for_each = {
    for name, machine in var.machines :
    name => machine
    if machine.node_type == "master"
  }

  name = each.key
  image = each.image
  server_type = each.server_type
  location = each.location
  ssh_keys  = ["${data.hcloud_ssh_key.ssh_key.id}"]
}

当我跑的时候

代码语言:javascript
复制
$ terraform init
$ terraform apply -var-file tfvars.json -state terraform.tfstate -auto-approve

我得到以下错误

代码语言:javascript
复制
╷
│ Error: Invalid "each" attribute
│
│   on main.tf line 40, in resource "hcloud_server" "master":
│   40:   image = each.image
│
│ The "each" object does not have an attribute named "image". The supported
│ attributes are each.key and each.value, the current key and value pair of the
│ "for_each" attribute set.
╵
╷
│ Error: Invalid "each" attribute
│
│   on main.tf line 41, in resource "hcloud_server" "master":
│   41:   server_type = each.server_type
│
│ The "each" object does not have an attribute named "server_type". The
│ supported attributes are each.key and each.value, the current key and value
│ pair of the "for_each" attribute set.
╵
╷
│ Error: Invalid "each" attribute
│
│   on main.tf line 42, in resource "hcloud_server" "master":
│   42:   location = each.location
│
│ The "each" object does not have an attribute named "location". The supported
│ attributes are each.key and each.value, the current key and value pair of the
│ "for_each" attribute set. 

我想通过顶部的tfvars.json传递我的服务器的配置,但是这个错误阻止了我的执行。我可能遗漏了如何将json传递给terraform中的变量的概念,因此对文档的任何引用也是非常有用的。

EN

回答 1

Stack Overflow用户

发布于 2021-06-30 07:42:15

在我看来,您缺少each中的value。所以它应该是:

代码语言:javascript
复制
resource "hcloud_server" "master" {
  for_each = {
    for name, machine in var.machines :
    name => machine
    if machine.node_type == "master"
  }

  name = each.key
  image = each.value.image
  server_type = each.value.server_type
  location = each.value.location
  ssh_keys  = ["${data.hcloud_ssh_key.ssh_key.id}"]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68186694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档