When using Terraform and YAML pipelines in Azure DevOps, it’s not clear how to read the output variables of a Terraform configuration.

The basics

First, you have to name the task that you’re using to apply.

- task: TerraformTaskV1@0
  name: TerraformOutputs
  inputs:
    provider: 'azurerm'
    command: 'apply'

Then, the task will store the output variables in a json file in a certain file path. You can get this path through the jsonOutputVariablesPath variable.

Then you can use this Azure DevOps task output variable and read the file then parse it for values.

For example, here’s an example using bash reading the cluster_name and the resource_group_name output variables from Terraform.


- bash: |
    CLUSTER_NAME=$(cat $(TerraformOutputs.jsonOutputVariablesPath) | jq '.cluster_name.value' -r)
    RESOURCE_GROUP_NAME=$(cat $(TerraformOutputs.jsonOutputVariablesPath) | jq '.resource_group_name.value' -r)

How this works is this:

  • Using bash, we use cat to read the contents of the file
  • Then, we pipe the output to an utility called jq which will parse the raw string to json and allow us to filter its values
  • Applying a jq filter, we look for the raw value of those variables. For example: see this snippet
  • We assign the filtered value to a variable

Pipeline Variables

Once the value is parsed, it can then be assigned to a pipeline variable. Here’s how to do it from bash.

echo "##vso[task.setvariable variable=Outputs.ClusterName]$CLUSTER_NAME"