1

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 Answer 1

3

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]
Sign up to request clarification or add additional context in comments.

8 Comments

i have applied it and got this error: 2020-05-25T12:21:07.2672860Z [1m[31mError: [0m[0m[1mInvalid reference[0m 2020-05-25T12:21:07.2674089Z 2020-05-25T12:21:07.2675081Z [0m on network.tf line 8, in resource "azurerm_subnet" "Subnet-lab": 2020-05-25T12:21:07.2676095Z 8: address_prefix = [4mavailableSubnetList[0m[count.index] 2020-05-25T12:21:07.2676984Z [0m 2020-05-25T12:21:07.2700114Z A reference to a resource type must be followed by at least one attribute 2020-05-25T12:21:07.2701181Z access, specifying the resource name.
here $(availableSubnetList) seems empty at the logs: the terraform.exe plan -var=LabRGName=rg-deniztest3-87719dccbe -var=project_subnet_number=3 "-var=availableSubnetList=${split(,, )}" Although i enter -var="availableSubnetList=${split(",", $(availableSubnetList))}"
i noticed that i made an error in tf script: address_prefix = availableSubnetList[count.index] . It should be var.availableSubnetList[count.index]
@MoonHorse I'm not 100% sure the ${split()} expression works when passed at the command line, you might need to do it in the task itself
@MoonHorse That's great (sorry I couldn't be of more help with tf, not my strongsuit). I've updated my answer to reflect your solution as well :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.