我有一个列表,其中填充了地址前缀,例如 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有任何想法吗?
答案 0 :(得分:3)
从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]