如何使用 terraform plan 传递 env 变量

How to pass an env variable with terraform plan

我可以帮助解决以下问题。我正在尝试创建一个 vnet。

main.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.

所以将 cidr 直接传递到计划中作为 var 工作。 但是当我设置一个环境变量时它不会:

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

如果你想通过环境变量传递变量信息,另一个更直接的途径是使用 environment variable Terraform variable synatax。突出显示的优点之一是它在 运行 自动化时很有用。例如,

export TF_VAR_vnet_address_space=10.0.0.0/24
tf plan

TF_VAR_ 将被解释为变量前缀,而 vnet_address_space 将被视为变量输入。