Azure 如何通过地形平面图传递环境变量

Azure 如何通过地形平面图传递环境变量,azure,terraform,terraform-provider-azure,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" } variable "vnet_address_space" { } resource "azure

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

main.tf

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

variable "vnet_address_space" {
  type = "list"
}
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"
  }
}
vnet/vnet.tf

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

variable "vnet_address_space" {
  type = "list"
}
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.
有谁知道如何将env变量与计划一起传递

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

如果希望通过环境变量传递变量信息,另一个更直接的途径是使用。突出显示的优点之一是,在自动化中运行时,它可能很有用。比如说,

export TF\u VAR\u vnet\u address\u space=10.0.0.0/24
tf计划

TF\u VAR\u
将被解释为变量前缀,而
vnet\u address\u space
将被视为变量输入

感谢您在这里分享您的解决方案。您可以接受这个答案,以便其他人可以直接知道这是工作:-)谢谢