1
votes

I could use a hand with the below issue. i am trying to create a 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:

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.

So passing the cidr right into the plan as var works. But when I set an env variable it doesn't:

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.

Even though the env is set correcty.

 echo $vnet_address_space
10.0.0.0/24

Anyone who knows how to pass the env variable along with the plan?

2

2 Answers

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

This way the plan works locally... but still not via my azure devops pipeline:

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

This actually did the trick:

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

Another more direct route if you want to pass variable information via an environment variable is to use the environment variable Terraform variable synatax. One of the highlighted advantages is that it can be useful when running in automation. For example,

export TF_VAR_vnet_address_space=10.0.0.0/24
tf plan

The TF_VAR_ will be interpreted as a variable prefix and vnet_address_space will be treated as a variable input.