3
votes

I am trying to pass a list of strings as an azure pipeline variable to a terraform task.

I have a variable created for the pipeline called such that ips = [\"1.1.1.1\", \"2.2.2.2\"] and I can access this in the azure pipeline using $(ips)

I want to pass this variable as mentioned here in the official terraform documentation: terraform apply -var='image_id_list=["ami-abc123","ami-def456"]' to the terraform plan task

here is my terraform plan section of the 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'

when the pipeline runs, it fails at the terraform plan step with the error -

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

I have also tried various combinations of for declaring the azure pipeline variable like: ips = ["\"1.1.1.1\", \"2.2.2.2"\]

which also gives the same error -

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

I have tried to a lot of combinations of setting quotes or double quotes on the commandOptions value and also the ips variable but I am getting the same errors (or valid terraforms error).

It seems the first double quote in the azure variable seems to causing some issues. Any pointers o how to pass a list of double quoted strings as an azure pipeline variable input to terraform task is much appreciated. Thanks!

edit -

I hardcoded the values directly in the yaml but still getting errors.

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

and it basically does not accept the ip values and asks for a manual entry:

/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: 

again it seems there is a problem around the first double quote.

2
Do you have the same issue if you set the ips in your task directly instead of using variables?Walter
@WalterQian-MSFT i have added my observations as an edit in the original questionkpython
Unfortunately none of those links seems applicable to me... @Leo Liu-MSFTkpython

2 Answers

2
votes

Today I dealt with a similar problem and this answer helped me a lot.

In a nutshell, you should define the variable ips as ["\"1.1.1.1\"", "\"2.2.2.2\""], and terraform plan task needs to take the variable as the following -var=ips=$(ips).

Hope this helps anyone encountering a similar issue.

0
votes

how to pass a list of double quoted strings as an azure pipeline variable input to terraform task?

I am afraid there is no such way to pass double quoted strings to terraform task at this moment.

That because the double quoted could not be escaped by the \.

You could check below threads for some more details: