Terraform 无效的“each”属性

2024-03-05

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

{
  "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 中我想创建实例

############## 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}"]
}

当我跑步时

$ terraform init
$ terraform apply -var-file tfvars.json -state terraform.tfstate -auto-approve

我收到以下错误

╷
│ 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 中的变量的概念,因此对文档的任何引用也受到高度赞赏。


你失踪了value in each据我所知。所以应该是:

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}"]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Terraform 无效的“each”属性 的相关文章

随机推荐

  • SQL Server 2012 中的 group_concat 与 ORDER BY 另一列

    我有一个包含约一百万个条目的表 如下所示 customer id purchased at product 1 2012 06 01 00 00 apples 1 2012 09 02 00 00 apples 1 2012 10 01 0
  • 找不到内存泄漏

    我一直在开发 WP7 应用程序 它是图像库应用程序 实现了基本的缩放和轻拂手势 出于测试目的 我将离线图像 它们的文件名已编号 设置为 内容 来编译应用程序 并通过硬编码字符串 稍后将被替换 访问它们 但后来意识到应用程序消耗大量内存 我以
  • C++ - 将 istream_iterator 与 wstringstream 结合使用

    我正在尝试向我编写的程序添加 Unicode 支持 我的 ASCII 代码已编译并包含以下几行 std stringstream stream abc std istream iterator
  • 将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表

    我正在尝试使用产品的目录可见性值向管理产品列表添加自定义列 基本上 我需要更容易地知道哪些是隐藏的 哪些不是 到目前为止我的子主题的function php的代码 add filter manage edit product columns
  • 如何设置 GitHub.com 中的默认分支?

    我找到了以下信息 但没有找到哪里可以访问它 账户设置里没找到 有回购设置页面吗 您必须是回购所有者吗 https github com blog 421 pick your default branch https github com b
  • 承诺链内的返回值没有被调用

    我正在使用 Promise 库 Bluebird 目前遇到的问题是函数内的所有内容都运行良好 但是当我尝试返回值时 函数反而返回undefined 这是承诺链 function foo createGroupMembers parsedCh
  • 无法安装:错误 0x80070643:无法安装 MSI 包

    我正在尝试安装一个应用程序 msi 我使用 WiX 安装程序创建了一个 exe 它有依赖项 我尝试将其安装在某些电脑上 效果非常好 但在其中一个系统中 当它尝试安装 msi 时 它会给出错误并回滚安装 这是正常的 无法弄清楚为什么它没有安装
  • PHP - 服务器端变量将永远“存在”?

    我正在寻找一种简单的方法来在服务器内存中存储计数器以允许页面加载选择 有点 伪代码 if counter is odd then load page x else even counter load page y increment cou
  • 将用户定义的命令行参数添加到/?窗户

    使用 Inno Setup 可以添加用户定义的命令行参数 当我使用 HELP命令中未列出用户定义的参数 如何将带有描述的命令添加到 HELP window Inno Setup 6 0 支持HelpTextNote message http
  • Oracle CTE 合并

    我正在尝试使用 CTE 公用表表达式 的简单合并语句 但它给出了一个错误 MERGE INTO emp targ USING ERROR at line 4 ORA 00928 missing SELECT keyword 合并语句中不允许
  • 反距离加权插值

    我想计算一个重量作为距离的倒数 例如反距离加权插值 http en wikipedia org wiki Inverse distance weighting double wgt 0 wgt tmp result 0 for int i
  • 单一职责原则在 OOP 中有效吗?

    我正在努力理解如何将单一职责原则与 OOP 结合使用 如果我们要完全遵循这一原则 那么我们是否会留下许多类 其中许多类可能每个都只有一个方法 如果我们不严格遵循这个原则 那么这个原则还有什么意义呢 我喜欢这样陈述单一职责原则 你编写的每件事
  • x86 虚拟化如何工作

    任何人都可以推荐有关虚拟化的好读物吗 我建议英特尔软件开发人员手册 http www intel com products processor manuals 具体来说卷3B http www intel com Assets PDF ma
  • 没有名为“delta.tables”的模块

    我收到以下代码的错误 请帮忙 from delta tables import ModuleNotFoundError No module named delta tables INFO SparkContext Invoking stop
  • 启动 RPC 服务器时出现“端点重复”

    我的程序使用 Microsoft RPC 进行进程间通信 为了准备接收 RPC 调用 程序运行以下序列 RpcServerUseProtseqEp 然后 RpcServerRegisterIf 则 RpcServerListen 该程序按照
  • 如何理解这段由数字组成的 JavaScript 代码

    这是 JavaScript 代码 我认为它是十六进制形式并尝试对其进行解码 但仍然找不到该代码的含义 我在我的博客模板中使用此代码 我想了解这段javascript代码的真正含义
  • 再次:UnicodeEncodeError:ascii 编解码器无法编码

    我有一个我想要解析的 XML 文件的文件夹 我需要从这些文件的元素中获取文本 它们将被收集并打印到 CSV 文件中 其中的元素按列列出 I can实际上现在就这样做some我的文件 也就是说 对于我的许多 XML 文件 该过程进展顺利 并且
  • Page_ClientValidate 未定义 (ASP.NET MVC)

    我有一个 ASP NET MVC 表单 布局如下 using Html BeginForm null null FormMethod Post new id ccform Html ValidationSummary true Please
  • 从 bash 捕获 telnet 超时

    参考这个问题 使用 bash 脚本自动化 telnet 会话 https stackoverflow com questions 7013137 automating telnet session using bash scripts 我正
  • Terraform 无效的“each”属性

    我有一个 tfvars json 文件 我想用它来传递我的服务器配置 test test machines master01 node type master image ubuntu 20 04 server type cx21 loca