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

标签 azure powershell azure-devops terraform terraform-provider-azure

我有一个列表,其中充满了地址前缀,例如 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/

相关文章:

java - 在 Azure 应用服务中访问 Tomcat 和 Webapp 日志

azure - 从 Azure DevOps 池中删除代理

azure - 将数据从 Azure Blob 复制到 Azure Data Lake 的最佳(快速)方法

azure - 使用 Azure Functions 中的 Google API

string - 从 IPv4 地址的字符串表示中获取特定的八位字节

powershell - 如何使用 PowerShell 选择无标题 csv 文件的前 10 列?

javascript - 从 PowerShell 脚本运行 javascript 文档

azure - terraform 计划不与 azurerm 提供商合作

azure - 了解 Azure 发布管道

azure - 无法为 Sql Azure 数据库生成脚本 : "Getting the list of objects from : failed"