In terraform tfvars file can we put value somewhere else and then simply we can just recall?

huangapple go评论80阅读模式
英文:

In terraform tfvars file can we put value somewhere else and then simply we can just recall?

问题

我们是否可以将路由表的数值放在其他地方,例如其他的tfvars文件或变量/本地,然后在tfvars文件中只调用路由表 route_table = ""?

英文:

Can we put route table value somewhere else eg other tfvars file or varibles/local and then just in the tfvars file. we can call route table route_table =""

networks = {
  umms-security-vnet = {
    resource_group_name = "testrg" #required
    location            = "eastus"                      #required
    addressSpace = [
      "10.229.192.0/20" #required
    ]
    dnsServers = [
      "10.231.18.4",
      "146.189.24.10",
      "172.26.40.125"
    ]
    subnets = {
      GatewaySubnet = {
        addressPrefix = "10.229.192.0/24"
      }
      app-1-subnet = {
        addressPrefix                     = "10.229.198.0/24" #required
        route_table = {                                        #optional
          security-rt = {
            disableBgpRoutePropagation = false
            routes = [
              {
                name             = "default-udr"
                addressPrefix    = "0.0.0.0/0"
                nextHopType      = "VirtualAppliance"
                nextHopIpAddress = "10.231.10.100"
              },
              {
                name             = "network-146.189.0.0-16-udr"
                addressPrefix    = "146.189.0.0/16"
                nextHopType      = "VirtualAppliance"
                nextHopIpAddress = "10.231.10.200"
              }
            ]
          }
        }

答案1

得分: 0

可以将路由表的值存储在变量中,而不是直接在 tfvars 文件中。这允许您在变量中定义路由表的值,并在 tfvars 文件中引用它。

在这里是更新后的 Terraform 代码。

Main.tf

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "vnetrg" {
  name     = "VNET-RG"
  location = "eastus"
}

resource "azurerm_virtual_network" "security-vnet" {
  name                = "security-vnet"
  address_space       = var.address_space
  location            = azurerm_resource_group.vnetrg.location
  resource_group_name = azurerm_resource_group.vnetrg.name
}

resource "azurerm_subnet" "subnet-without-rt" {
  name                 = "subnet-without-rt"
  address_prefixes     = [var.subnet_prefixes[0]]
  virtual_network_name = azurerm_virtual_network.security-vnet.name
  resource_group_name  = azurerm_resource_group.vnetrg.name
}

resource "azurerm_subnet" "subnet-with-rt" {
  name                 = "subnet-with-rt"
  address_prefixes     = [var.subnet_prefixes[1]]
  virtual_network_name = azurerm_virtual_network.security-vnet.name
  resource_group_name  = azurerm_resource_group.vnetrg.name
}

resource "azurerm_subnet_route_table_association" "subnet-with-rt-association" {
  subnet_id      = azurerm_subnet.subnet-with-rt.id
  route_table_id = azurerm_route_table.security-rt.id
}

resource "azurerm_route_table" "security-rt" {
  name                = "security-rt"
  location            = azurerm_resource_group.vnetrg.location
  resource_group_name = azurerm_resource_group.vnetrg.name

  dynamic "route" {
    for_each = var.route_table["security-rt"].routes
    content {
      name             = route.value.name
      address_prefix   = route.value.addressPrefix
      next_hop_type    = route.value.nextHopType
      next_hop_in_ip_address = route.value.nextHopIpAddress
    }
  }
}

variables.tf

variable "address_space" {
  description = "虚拟网络的地址空间"
  type        = list(string)
}

variable "subnet_prefixes" {
  description = "子网的前缀"
  type        = list(string)
}

variable "route_table" {
  description = "路由表配置"
  type        = map(object({
    disableBgpRoutePropagation = bool
    routes = list(object({
      name             = string
      addressPrefix    = string
      nextHopType      = string
      nextHopIpAddress = string
    }))
  }))
  default     = {
    security-rt = {
      disableBgpRoutePropagation = false
      routes = [
        {
          name             = "default-udr"
          addressPrefix    = "0.0.0.0/0"
          nextHopType      = "VirtualAppliance"
          nextHopIpAddress = "10.231.10.100"
        },
        {
          name             = "network-146.189.0.0-16-udr"
          addressPrefix    = "146.189.0.0/16"
          nextHopType      = "VirtualAppliance"
          nextHopIpAddress = "10.231.10.200"
        }
      ]
    }
  }
}

terraform.tfvars

address_space = ["10.229.192.0/20"]

subnet_prefixes = ["10.229.192.0/24", "10.229.198.0/24"]

route_table = {
  "security-rt" = {
    disableBgpRoutePropagation = false
    routes = [
      {
        name             = "default-udr"
        addressPrefix    = "0.0.0.0/0"
        nextHopType      = "VirtualAppliance"
        nextHopIpAddress = "10.231.10.100"
      },
      {
        name             = "network-146.189.0.0-16-udr"
        addressPrefix    = "146.189.0.0/16"
        nextHopType      = "VirtualAppliance"
        nextHopIpAddress = "10.231.10.200"
      }
    ]
  }
}

route_table 变量在 variables.tf 块中定义,其值在 tfvars 文件中使用 var.route_table["security-rt"] 进行引用。

英文:

> Can we put route table value somewhere else eg other tfvars file or varibles/local and then just in the tfvars file. we can call route table route_table

Yes, you can store the value of the route table in variables instead of directly in the tfvars file. This allows you to define the route table value in variables and reference it in tfvars file.

Here is the updated Terraform code.

Main.tf

provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "vnetrg" {
name     = "VNET-RG"
location = "eastus"
}
resource "azurerm_virtual_network" "security-vnet" {
name                = "security-vnet"
address_space       = var.address_space
location            = azurerm_resource_group.vnetrg.location
resource_group_name = azurerm_resource_group.vnetrg.name
}
resource "azurerm_subnet" "subnet-without-rt" {
name                 = "subnet-without-rt"
address_prefixes     = [var.subnet_prefixes[0]]
virtual_network_name = azurerm_virtual_network.security-vnet.name
resource_group_name  = azurerm_resource_group.vnetrg.name
}
resource "azurerm_subnet" "subnet-with-rt" {
name                 = "subnet-with-rt"
address_prefixes     = [var.subnet_prefixes[1]]
virtual_network_name = azurerm_virtual_network.security-vnet.name
resource_group_name  = azurerm_resource_group.vnetrg.name
}
resource "azurerm_subnet_route_table_association" "subnet-with-rt-association" {
subnet_id      = azurerm_subnet.subnet-with-rt.id
route_table_id = azurerm_route_table.security-rt.id
}
resource "azurerm_route_table" "security-rt" {
name                = "security-rt"
location            = azurerm_resource_group.vnetrg.location
resource_group_name = azurerm_resource_group.vnetrg.name
dynamic "route" {
for_each = var.route_table["security-rt"].routes
content {
name             = route.value.name
address_prefix   = route.value.addressPrefix
next_hop_type    = route.value.nextHopType
next_hop_in_ip_address = route.value.nextHopIpAddress
}
}
}

variables.tf

variable "address_space" {
description = "Address space for the virtual network"
type        = list(string)
}
variable "subnet_prefixes" {
description = "Prefixes for the subnets"
type        = list(string)
}
variable "route_table" {
description = "Route table configuration"
type        = map(object({
disableBgpRoutePropagation = bool
routes = list(object({
name             = string
addressPrefix    = string
nextHopType      = string
nextHopIpAddress = string
}))
}))
default     = {
security-rt = {
disableBgpRoutePropagation = false
routes = [
{
name             = "default-udr"
addressPrefix    = "0.0.0.0/0"
nextHopType      = "VirtualAppliance"
nextHopIpAddress = "10.231.10.100"
},
{
name             = "network-146.189.0.0-16-udr"
addressPrefix    = "146.189.0.0/16"
nextHopType      = "VirtualAppliance"
nextHopIpAddress = "10.231.10.200"
}
]
}
}
}

terraform.tfvars

address_space = ["10.229.192.0/20"]
subnet_prefixes = ["10.229.192.0/24", "10.229.198.0/24"]
route_table = {
"security-rt" = {
disableBgpRoutePropagation = false
routes = [
{
name             = "default-udr"
addressPrefix    = "0.0.0.0/0"
nextHopType      = "VirtualAppliance"
nextHopIpAddress = "10.231.10.100"
},
{
name             = "network-146.189.0.0-16-udr"
addressPrefix    = "146.189.0.0/16"
nextHopType      = "VirtualAppliance"
nextHopIpAddress = "10.231.10.200"
}
]
}
}

Theroute_table variable is defined in variables.tf block, and its value is referenced in the tfvars file using var.route_table["security-rt"].

Terraform Apply

In terraform tfvars file can we put value somewhere else and then simply we can just recall?

Once the ran above Terraform code, the resources are created successfully as below.

In terraform tfvars file can we put value somewhere else and then simply we can just recall?

Reference: azurestack_virtual_network

huangapple
  • 本文由 发表于 2023年5月11日 13:38:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76224424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定