我正在尝试使用变量文件在 Azure 中使用 Terraform 部署资源组,但如果我只有一个变量,它就可以工作。如果我使用两个,我会收到一个错误:
“标志-var-file 的无效值“variables.tf”:变量不支持多个映射声明”
变量文件如下:
variable "resource_group_name" {
description = "The name of the resource group in which the resources will be created"
default = "im-from-the-variables-file"
}
variable "location" {
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
default = "west europe"
}
Run Code Online (Sandbox Code Playgroud)
用于部署的主要文件如下:
resource "azurerm_resource_group" "vm" {
name = "${var.resource_group_name}"
location = "${var.location}"
}
Run Code Online (Sandbox Code Playgroud)
您已经将变量定义语法与变量设置语法混淆了。
Terraform 将连接.tf目录中的所有文件,以便您的variables.tf文件(假设它与您的main.tf(或包含您的azurerm_resource_group资源等的任何内容)位于同一目录中)已经包含在内。
您需要先定义每个变量,然后才能使用它,例如:
resource "azurerm_resource_group" "vm" {
name = "${var.resource_group_name}"
location = "${var.location}"
}
Run Code Online (Sandbox Code Playgroud)
由于变量resource_group_name和location未定义,它们本身是无效的。
您可以使用在variables.tf文件中使用的语法定义变量:
variable "location" {
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
default = "west europe"
}
Run Code Online (Sandbox Code Playgroud)
要覆盖默认值(如果需要或未提供默认值),则您需要在运行时传递变量(使用TF_VAR_location环境变量或使用-var location="west us"),或者您可以定义采用以下形式的 vars 文件:
location = "west us"
resource_group_name = "im-from-the-variables-file"
Run Code Online (Sandbox Code Playgroud)
Terraform 将自动加载名为terraform.tfvarsor的目录中的任何文件,*.auto.tfvars您也可以在任何时候定义额外的 vars 文件,-var-file=myvars.tfvars就像您尝试做的那样(但使用.tf包含 HCL 而不是密钥对的文件。
| 归档时间: |
|
| 查看次数: |
1746 次 |
| 最近记录: |