Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免在Terraform中重复变量?_Terraform - Fatal编程技术网

如何避免在Terraform中重复变量?

如何避免在Terraform中重复变量?,terraform,Terraform,Terraform不允许在变量文件中插入变量,否则会出现错误: 错误:不允许使用变量 在variables.tf第9行的变量“resource\u group\u name”中:9: default=“${var.prefix}-terraform-dev_rg” 这里不能使用变量 这就意味着,当我试图为资源组创建名称时,我会复制variables.tf文件中前缀的值 有没有一个很好的方法来避免重复变量的值 变量。tf variable "prefix" { description = "T

Terraform不允许在变量文件中插入变量,否则会出现错误:

错误:不允许使用变量

在variables.tf第9行的变量“resource\u group\u name”中:9:
default=“${var.prefix}-terraform-dev_rg”

这里不能使用变量

这就意味着,当我试图为资源组创建名称时,我会复制variables.tf文件中前缀的值

有没有一个很好的方法来避免重复变量的值

变量。tf

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_name {
  type = "string"
  default = "terraform-dev_rg"
}

variable resource_group_location {
  type = "string"
  default = "eastus"
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  version         = "=1.28.0"
}

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = var.resource_group_name
  location = var.resource_group_location
}

#Create an application gateway with web app firewall
module "firewall" {
  source                  = "./firewall"
  resource_group_name     = var.resource_group_name
  resource_group_location = var.resource_group_location
}
#Passed down from the root variables.tf
variable "prefix" {}
variable "resource_group_name" {}
variable "resource_group_location" {}
# Create a virtual network for the firewall
resource "azurerm_virtual_network" "firewall-vnet" {
  name = "${var.prefix}-waf-vnet"
  address_space = ["10.0.0.0/16"]
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location
}
main.tf

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_name {
  type = "string"
  default = "terraform-dev_rg"
}

variable resource_group_location {
  type = "string"
  default = "eastus"
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  version         = "=1.28.0"
}

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = var.resource_group_name
  location = var.resource_group_location
}

#Create an application gateway with web app firewall
module "firewall" {
  source                  = "./firewall"
  resource_group_name     = var.resource_group_name
  resource_group_location = var.resource_group_location
}
#Passed down from the root variables.tf
variable "prefix" {}
variable "resource_group_name" {}
variable "resource_group_location" {}
# Create a virtual network for the firewall
resource "azurerm_virtual_network" "firewall-vnet" {
  name = "${var.prefix}-waf-vnet"
  address_space = ["10.0.0.0/16"]
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location
}
/firewall/variables.tf

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_name {
  type = "string"
  default = "terraform-dev_rg"
}

variable resource_group_location {
  type = "string"
  default = "eastus"
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  version         = "=1.28.0"
}

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = var.resource_group_name
  location = var.resource_group_location
}

#Create an application gateway with web app firewall
module "firewall" {
  source                  = "./firewall"
  resource_group_name     = var.resource_group_name
  resource_group_location = var.resource_group_location
}
#Passed down from the root variables.tf
variable "prefix" {}
variable "resource_group_name" {}
variable "resource_group_location" {}
# Create a virtual network for the firewall
resource "azurerm_virtual_network" "firewall-vnet" {
  name = "${var.prefix}-waf-vnet"
  address_space = ["10.0.0.0/16"]
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location
}
/firewall/main.tf

variable "prefix" {
  description = "The prefix used for all resources in this plan"
  default     = "terraform-dev"
}

variable resource_group_name {
  type = "string"
  default = "terraform-dev_rg"
}

variable resource_group_location {
  type = "string"
  default = "eastus"
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
  version         = "=1.28.0"
}

# Create a resource group
resource "azurerm_resource_group" "resource-group" {
  name     = var.resource_group_name
  location = var.resource_group_location
}

#Create an application gateway with web app firewall
module "firewall" {
  source                  = "./firewall"
  resource_group_name     = var.resource_group_name
  resource_group_location = var.resource_group_location
}
#Passed down from the root variables.tf
variable "prefix" {}
variable "resource_group_name" {}
variable "resource_group_location" {}
# Create a virtual network for the firewall
resource "azurerm_virtual_network" "firewall-vnet" {
  name = "${var.prefix}-waf-vnet"
  address_space = ["10.0.0.0/16"]
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location
}
尝试使用本地值,

变量“前缀”{
description=“用于此计划中所有资源的前缀”
default=“地形开发”
}
可变资源组位置{
type=“string”
default=“eastus”
}
当地人{
资源组名称=“${var.prefix}\u rg”
}
资源“azurerm\u资源组”“资源组”{
name=local.resource\u group\u name
位置=变量资源组位置
}