如何使用 Azure Devops 将 powershell 列表变量传输到 terraform?

How to transfer a powershell list variable to terraform by using Azure Devops?

我有一个列表,其中充满了地址前缀,例如 192.168.1.0/24

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

在 powershell 中创建并传输到我已经在 Azure Devops 中定义的 azure devops 管道变量。 我正在使用这个在 运行 和

上设置的变量
terraform apply -var="availableSubnetList=$(availableSubnetList)" 

在我的 Terraform 任务中创建一个子网。以下是 tf 脚本:

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

当我执行管道时,我在 terraform apply 任务上遇到以下错误:

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,Azure Devops 中的变量是字符串。您是否知道如何使用 Azure Devops 将 powershell 列表正确地传输到 terraform?

从 PowerShell 脚本输出以逗号分隔的子网字符串:

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

然后在调用terraform的时候再拆分成一个列表:

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

或者更改您在 terraform 中解析每个子网的方式:

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