gpt4 book ai didi

azure - 如何使用 terraform plan 传递环境变量

转载 作者:行者123 更新时间:2023-12-03 01:00:53 31 4
gpt4 key购买 nike

我可以用一只手来解决以下问题。我正在尝试创建一个虚拟网络。

ma​​in.tf

module "vnet" {
source = "./vnet"
vnet_address_space = var.vnet_address_space
}

variable "vnet_address_space" {
type = "list"
}

vnet/vnet.tf

variable "vnet_address_space" {
}

resource "azurerm_resource_group" "kubernetes" {
name = "bram-test2"
location = "westeurope"
tags = {
Team = "Platform"
Tool = "Terraform"
}
}

resource "azurerm_virtual_network" "kubernetes" {
name = "vnet"
location = "westeurope"
resource_group_name = "${azurerm_resource_group.kubernetes.name}"
address_space = var.vnet_address_space
tags = {
Team = "Platform"
Tool = "Terraform"
}
}

地形规划:

terraform plan -var 'vnet_address_space=["10.0.0.0/24"]'

Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage.
------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # module.vnet.azurerm_resource_group.kubernetes will be created + resource "azurerm_resource_group" "kubernetes" {
+ id = (known after apply)
+ location = "westeurope"
+ name = "bram-test2"
+ tags = {
+ "Team" = "Platform"
+ "Tool" = "Terraform"
}
} # module.vnet.azurerm_virtual_network.kubernetes will be created + resource "azurerm_virtual_network" "kubernetes" {
+ address_space = [
+ "10.0.0.0/24",
]
+ id = (known after apply)
+ location = "westeurope"
+ name = "vnet"
+ resource_group_name = "bram-test2"
+ tags = {
+ "Team" = "Platform"
+ "Tool" = "Terraform"
}
+ subnet {
+ address_prefix = (known after apply)
+ id = (known after apply)
+ name = (known after apply)
+ security_group = (known after apply)
}
} Plan: 2 to add, 0 to change, 0 to destroy.

因此,当 var 起作用时,将 cidr 直接传递到计划中。但是当我设置环境变量时它不会:

terraform plan -var 'vnet_address_space=["${vnet_address_space}"]'

Error: Variables not allowed
on <value for var.vnet_address_space> line 1:
(source code not available)
Variables may not be used here.
Error: No value for required variable
on main.tf line 6:
6: variable "vnet_address_space" {
The root module input variable "vnet_address_space" is not set, and has no
default value. Use a -var or -var-file command line argument to provide a
value for this variable.

即使环境设置正确。

 echo $vnet_address_space
10.0.0.0/24

有人知道如何将环境变量与计划一起传递吗?

最佳答案

terraform plan -var "vnet_address_space=[${vnet_address_space}]"

这样该计划就可以在本地运行...但仍然不能通过我的 azure devops 管道:

Error: Invalid number literal

on <value for var.vnet_address_space> line 1:
(source code not available)

Failed to recognize the value of this number literal.

##[error]Bash exited with code '1'.

这实际上起到了作用:

terraform plan -var "vnet_address_space=[\"${vnet_address_space}\"]"

关于azure - 如何使用 terraform plan 传递环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60352352/

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