如何将双引号字符串列表作为azure管道变量输入传递到terraform任务?

时间:2020-10-17 22:45:41

标签: azure-devops yaml terraform azure-pipelines

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

我为管道创建了一个变量,名为ips = [\"1.1.1.1\", \"2.2.2.2\"],我可以使用$(ips)在天青管道中访问它

我想按照官方terraform文档中的here传递此变量: terraform apply -var='image_id_list=["ami-abc123","ami-def456"]'到Terraform Plan任务

这是我的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计划步骤失败,并显示错误-

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变量中的第一个双引号似乎引起了一些问题。非常感谢任何将如何将双引号字符串列表作为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: 

再次在第一个双引号周围出现问题。

2 个答案:

答案 0 :(得分:1)

今天我处理了类似的问题,this answer对我有很大帮助。

简而言之,您应该将变量status定义为status,并且Terraform Plan任务需要将该变量作为以下ips

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

答案 1 :(得分:0)

如何将双引号字符串列表作为azure管道变量输入传递给terraform任务?

现在恐怕还没有办法将双引号字符串传递给terraform任务。

那是因为\无法将双引号转义。

您可以检查以下线程以获取更多详细信息: