简体   繁体   English

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

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

I have a list which is filled with addressprefixes like 192.168.1.0/24我有一个列表,其中充满了地址前缀,如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.在 powershell 中创建并转移到我已经在 Azure Devops 中定义的 azure devops 管道变量。 在此处输入图像描述 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.在我的 Terraform 任务中创建子网。 Here below is the tf script:下面是 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]
}

When i executed the pipeline, i am having the following error on terraform apply task:当我执行管道时,在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, the variables in Azure Devops are strings. Afaik,Azure Devops 中的变量是字符串。 Do you have any idea on how to transfer a powershell list correctly to terraform by using Azure Devops?您对如何使用 Azure Devops 将 powershell 列表正确传输到 terraform 有任何想法吗?

Output a comma-separated string of subnets from the PowerShell script: Output 来自 PowerShell 脚本的以逗号分隔的子网字符串:

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

Then split it into a list again when invoking terraform:然后在调用 terraform 时再次拆分成一个列表:

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

Or change the way you resolve each subnet in terraform:或者更改您在 terraform 中解析每个子网的方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM