我对地形很陌生。
我有一个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 ###############
# 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我得到以下错误
│ 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. 警告给出了一些提示,我需要在根模块中提到这个变量,但是我甚至不知道我的根模块是什么,为什么不能像我现在这样传递变量呢?
发布于 2021-06-28 22:09:05
您的错误信息:
一个名为“”的输入变量未被声明为
因此,您需要在main.tf文件中添加machines变量声明:
# machines variable
variable "machines" {}https://stackoverflow.com/questions/68169800
复制相似问题