'Export a multi-line variable to a file in azure-devops yaml pipeline

I have a key-vault secret called mySecret

mySecret:

foo="val1"
bar="val2"
baz="val3"

my pipeline shall create a file with mySecret values

variables:  
- group: myKeyVault

steps:
  - bash: |
      echo "##vso[task.setvariable variable=tfvars]$(mySecret)"
      cat <<< "$(tfvars)" > terraform.tfvars
    displayName: export mySecret

Problem 1: cat <<< "$(tfvars)" > terraform.tfvars shall not echo out $tfvars in the pipeline log. Even $mySecret doesn't hold any secret content.

Problem 2: terraform.tfvars remain empty.

The overall idea is - you guessed it - to create terraform.tfvars file from a key-vault.



Solution 1:[1]

Solved by reading the keyvault value over the cli:

parameters:
  - name: environment
    default: 'production'
  - name: keyvault
    default: 'myKeyVault'

steps:
  - bash: |
      az keyvault secret show \
        --name "${{ parameters.environment }}-tfvars" \
        --vault-name "${{ parameters.keyvault }}" \
        --query "value" -otsv > ${{ parameters.environment }}.auto.tfvars
    displayName: Set Variables

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1