1
votes

I have a list which is filled with addressprefixes like 192.168.1.0/24

$listOfSubnetsToBeCreated = @() 
Write-Host "##vso[task.setvariable variable=availableSubnetList]$listOfSubnetsToBeCreated"

created in a powershell and transferred to azure devops pipeline variable that i have already defined in Azure Devops. enter image description here I am using this variable which is set on the run with

terraform apply -var="availableSubnetList=$(availableSubnetList)" 

in my Terraform task to create a subnet. Here below is the tf script:

variable "availableSubnetList" {
  type = list(string)
  default = [""]
}
resource "azurerm_subnet" "Subnet-lab" {
    count = var.project_subnet_number
    name = join("-", ["${var.LabRGName}","subnet", "${count.index + 1}"])
    resource_group_name = var.AdminRGName
    virtual_network_name = var.lab_vnet
    address_prefix = var.availableSubnetList[count.index]
}

When i executed the pipeline, i am having the following error on terraform apply task:

2020-05-25T14:39:52.5509261Z [0m  on <value for var.availableSubnetList> line 1:
2020-05-25T14:39:52.5510090Z   (source code not available)
2020-05-25T14:39:52.5510378Z 
2020-05-25T14:39:52.5510814Z This character is not used within the language.
2020-05-25T14:39:52.5511149Z [0m[0m
2020-05-25T14:39:52.5511412Z [31m
2020-05-25T14:39:52.5512135Z [1m[31mError: [0m[0m[1mInvalid expression[0m
2020-05-25T14:39:52.5512438Z 
2020-05-25T14:39:52.5512833Z [0m  on <value for var.availableSubnetList> line 1:
2020-05-25T14:39:52.5513301Z   (source code not available)
2020-05-25T14:39:52.5513582Z 
2020-05-25T14:39:52.5513977Z Expected the start of an expression, but found an invalid expression token.
2020-05-25T14:39:52.5514566Z [0m[0m

Afaik, the variables in Azure Devops are strings. Do you have any idea on how to transfer a powershell list correctly to terraform by using Azure Devops?

1

1 Answers

3
votes

Output a comma-separated string of subnets from the PowerShell script:

Write-Host "##vso[task.setvariable variable=availableSubnetList]$($listOfSubnetsToBeCreated -join ',')"

Then split it into a list again when invoking terraform:

terraform apply -var='availableSubnetList=${split(",", $(availableSubnetList))}'

Or change the way you resolve each subnet in terraform:

address_prefix = split(",", var.availableSubnetList)[count.index]