如何通过Terraform计划传递环境变量

时间:2020-02-22 12:55:16

标签: azure terraform terraform-provider-azure

我可以帮忙解决以下问题。我正在尝试创建一个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.

因此,像var一样,将cidr正确地传递到计划中。 但是,当我设置一个env变量时,它不会:

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

谁知道如何将env变量与计划一起传递?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

如果要通过环境变量传递变量信息,另一种更直接的方法是使用environment variable Terraform variable synatax。突出的优点之一是它在自动化运行时很有用。例如,

export TF_VAR_vnet_address_space=10.0.0.0/24
tf plan

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

相关问题