如何使用 Azure Devops 将 powershell 列表变量传输到 terraform? [英] How to transfer a powershell list variable to terraform by using Azure Devops?

查看:32
本文介绍了如何使用 Azure Devops 将 powershell 列表变量传输到 terraform?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list which is filled with addressprefixes like 192.168.1.0/24

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

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

created in a powershell and transferred to azure devops pipeline variable that i have already defined in Azure Devops. I am using this variable which is set on the run with

terraform apply -var="availableSubnetList=$(availableSubnetList)" 

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

in my Terraform task to create a subnet. Here below is the tf script:

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 任务出现以下错误:

When i executed the pipeline, i am having the following error on terraform apply task:

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

Afaik, the variables in Azure Devops are strings. Do you have any idea on how to transfer a powershell list correctly to terraform by using Azure Devops?

推荐答案

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

Output a comma-separated string of subnets from the PowerShell script:

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

然后在调用 terraform 时再次将其拆分为列表:

Then split it into a list again when invoking terraform:

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

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

Or change the way you resolve each subnet in terraform:

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

这篇关于如何使用 Azure Devops 将 powershell 列表变量传输到 terraform?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆