首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terraform没有找到我传递的json变量。

Terraform没有找到我传递的json变量。
EN

Stack Overflow用户
提问于 2021-06-28 20:43:54
回答 1查看 878关注 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 ###############

# 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
复制
│ Warning: Value for undeclared variable
│ 
│ The root module does not declare a variable named "machines" but a value was found in file "tfvars.json".
│ If you meant to use this value, add a "variable" block to the configuration.
│ 
│ To silence these warnings, use TF_VAR_... environment variables to provide certain "global" settings to all
│ configurations in your organization. To reduce the verbosity of these warnings, use the -compact-warnings
│ option.
╵
╷
│ Error: Reference to undeclared input variable
│ 
│   on main.tf line 31, in resource "hcloud_server" "master":
│   31:     for name, machine in var.machines :
│ 
│ An input variable with the name "machines" has not been declared. This variable can be declared with a
│ variable "machines" {} block. 

警告给出了一些提示,我需要在根模块中提到这个变量,但是我甚至不知道我的根模块是什么,为什么不能像我现在这样传递变量呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-28 22:09:05

您的错误信息:

一个名为“”的输入变量未被声明为

因此,您需要在main.tf文件中添加machines变量声明:

代码语言:javascript
复制
# machines variable
variable "machines" {}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68169800

复制
相关文章

相似问题

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