簡體   English   中英

如何使用 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 應用任務上出現以下錯誤:

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 有任何想法嗎?

Output 來自 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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM