gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:56:42 25 4
gpt4 key购买 nike

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

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

在 powershell 中创建并传输到我已在 Azure Devops 中定义的 azure Devops 管道变量。 奥 git _a我正在使用这个在运行时设置的变量

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]

关于azure - 如何使用 Azure Devops 将 powershell 列表变量传输到 terraform?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62000885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com