gpt4 book ai didi

azure - Terraform 变量文件

转载 作者:行者123 更新时间:2023-12-02 00:33:24 25 4
gpt4 key购买 nike

我正在尝试使用变量文件通过 Terraform 在 Azure 中部署资源组,但如果我只有一个变量,它就可以工作。如果我使用两个,则会出现错误:

"invalid value "variables.tf" for flag -var-file: multiple map declarations not supported for variables"

变量文件如下:

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

用于部署的主要文件如下:

resource "azurerm_resource_group" "vm" {
name = "${var.resource_group_name}"
location = "${var.location}"
}

最佳答案

您混淆了变量定义语法和变量设置语法。

Terraform 将连接一个目录中的所有 .tf 文件,以便您的 variables.tf 文件(假设它与您的 main.tf (或包含您的 azurerm_resource_group 资源等的任何内容)已包含在内。

您需要先定义每个变量,然后才能使用它,例如:

resource "azurerm_resource_group" "vm" {
name = "${var.resource_group_name}"
location = "${var.location}"
}

它们本身无效,因为变量resource_group_namelocation未定义。

您可以使用 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"
}

要覆盖默认值(如果需要或未提供默认值),您需要在运行时传递变量(使用 TF_VAR_location 环境变量或使用 - var location="west us") 或者您可以定义采用以下形式的 vars 文件:

location = "west us"
resource_group_name = "im-from-the-variables-file"

Terraform 将自动加载名为 terraform.tfvars*.auto.tfvars 的目录中的任何文件,您还可以随时使用 -var-file=myvars.tfvars 正如您尝试执行的那样(但使用包含 HCL 而不是 key 对的 .tf 文件。

关于azure - Terraform 变量文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50856044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com