I am trying to read the value of a variable from my Terraform outputs.tf
configuration file.
I have these 3 files in my Terraform project:
azure-pipelines.yaml
- my Azure pipeline YAML definitionlocals.tf
- parse my azure-pipelines.yaml
file with yamldecode
and convert to a Terraform-compatible representationoutputs.tf
- print details of my Terraform resource/datasource/local/variableMy sole challenge here is to read the value of the Resource Group variable (and Parameter values also) from the azure-pipelines.yaml
file and then write/display this in my outputs.tf
Terraform configuration.
So far, the numerous attempts I've made, including the two examples currently shown in my outputs.tf
file, have all failed to yield the desired result. Any idea how I can resolve this?
azure-pipelines.yaml
:
name: "Terraform Pipeline_$(Date:yyyyMMdd)$(Rev:.r)"
trigger:
branches:
include:
- main
parameters:
- name: name
type: string
default: testdata
- name: publishTestData
type: boolean
default: false
displayName: Publish test data (optional)
variables:
- name: ResourceGroup
value: 'rg-handsome-parakeet'
- name: subscriptionId
value: xxxx-36xx-xxe9-x3x4-46xxxx400
- name: backendServiceArm
value: 'MyADOServicePrincipal'
- name: storageAccountContainer
value: 'terraform'
- name: storageAccountKey
value: 'tf.apim.tfstate'
locals.tf
:
locals {
config = yamldecode(file("azure-pipelines.yaml"))
}
outputs.tf
:
output "get_local_file_variable" {
#value = local.config.variables["ResourceGroup"]
#value = local.config.${{variables.ResourceGroup}}
}
You can iterate over the variables and store the value when the name is ResourceGroup
:
locals {
config = yamldecode(file("azure-pipelines.yaml"))
}
locals {
resource_group = [for var in local.config.variables : var.value if var.name == "ResourceGroup"][0]
}
output "get_local_file_variable" {
value = local.resource_group
}
$ terraform plan
Changes to Outputs:
+ get_local_file_variable = "rg-needed-parakeet"