首页 > 解决方案 > 如何将双引号字符串列表作为天蓝色管道变量输入传递给 terraform 任务?

问题描述

我正在尝试将字符串列表作为 azure 管道变量传递给 terraform 任务。

我有一个为管道创建的变量,称为这样,ips = [\"1.1.1.1\", \"2.2.2.2\"]我可以在 azure 管道中使用$(ips)

我想通过官方 terraform 文档中提到的这个变量: toterraform apply -var='image_id_list=["ami-abc123","ami-def456"]' the terraform plan task

这是我的 yaml 地形计划部分:

        - task: TerraformTaskV1@0
          inputs:
            provider: 'azurerm'
            command: 'plan'
            commandOptions: '--var-file variables/$(Build.SourceBranchName)/$(Build.SourceBranchName).tfvars  -var=''allowips=$(ips)''' 
            environmentServiceNameAzureRM: $(linked_service)
          displayName: 'Terraform plan'

当管道运行时,它在 terraform plan 步骤失败并出现错误 -

terraform plan --var-file variables/dev/dev.tfvars -var='allowips=[ \1.1.1.1", "2.2.2.2" ]'
stat \1.1.1.1", "2.2.2.2" ]': no such file or directory
##[error]Error: The process '/opt/hostedtoolcache/terraform/0.12.28/x64/terraform' failed with exit code 1

我还尝试了各种组合来声明 azure 管道变量,例如: ips = ["\"1.1.1.1\", \"2.2.2.2"\]

这也给出了同样的错误 -

terraform plan --var-file variables/dev/dev.tfvars -var='allowips=[ "1.1.1.1", "2.2.2.2" ]'
stat "1.1.1.1", "2.2.2.2" ]': no such file or directory
##[error]Error: The process '/opt/hostedtoolcache/terraform/0.12.28/x64/terraform' failed with exit code 1

我尝试了很多在commandOptions值和ips变量上设置引号或双引号的组合,但我得到了相同的错误(或有效的 terraforms 错误)。

似乎 azure 变量中的第一个双引号似乎引起了一些问题。非常感谢任何关于如何将双引号字符串列表作为天蓝色管道变量输入传递给 terraform 任务的指针。谢谢!

编辑 -

我直接在 yaml 中硬编码了这些值,但仍然出现错误。

commandOptions: '--var-file variables/$(Build.SourceBranchName)/$(Build.SourceBranchName).tfvars -var=''allowips=[\"1.1.1.1\", \"2.2.2.2\"]'''

它基本上不接受 ip 值并要求手动输入:

/opt/hostedtoolcache/terraform/0.12.28/x64/terraform plan --var-file variables/dev/dev.tfvars -var='allowips=[\1.1.1.1", "2.2.2.2"]'
var.allowips
  Enter a value: 

同样,第一个双引号似乎存在问题。

标签: azure-devopsyamlterraformazure-pipelines

解决方案


今天我处理了一个类似的问题,这个答案对我帮助很大。

简而言之,您应该将变量定义ips["\"1.1.1.1\"", "\"2.2.2.2\""],而 terraform plan 任务需要将变量取为以下-var=ips=$(ips)

希望这可以帮助遇到类似问题的任何人。


推荐阅读