admin管理员组

文章数量:1278918

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 definition
  • locals.tf - parse my azure-pipelines.yaml file with yamldecode and convert to a Terraform-compatible representation
  • outputs.tf - print details of my Terraform resource/datasource/local/variable

My 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}}

}

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 definition
  • locals.tf - parse my azure-pipelines.yaml file with yamldecode and convert to a Terraform-compatible representation
  • outputs.tf - print details of my Terraform resource/datasource/local/variable

My 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}}

}
Share Improve this question edited Feb 25 at 10:39 hitman126 asked Feb 25 at 10:25 hitman126hitman126 9511 gold badge27 silver badges59 bronze badges 4
  • Thanks Paolo. Had expected to get a resolution without the need for an additional line of code in the locals.tf configuration, but your suggestion worked great. Perhaps the only downside for me is that if for instance I have 10 other variables to reference, this would mean I have to create 10 extra local blocks in my TF code, which would be a bit of an overkill..........unless there's some other workaround that can allow the same line of code to be reused or iterated over. – hitman126 Commented Feb 25 at 10:56
  • You could just put the result into the value of the output, i.e, value = [for var in local.config.variables : var.value if var.name == "ResourceGroup"][0]. – Marko E Commented Feb 25 at 10:59
  • Marko, my issue is that if I have to inject the remaining variables (e.g. backendServiceArm, storageAccountContainer, etc) into my outputs.tf file, it means I have to replicate local blocks for each of these variables, just like the Resource Group implementation. Without perhaps stating the obvious, one thing to point out is that my outputs.tf config is simply to enable me verify my yamlencode implementation, as the real objective is to pass those multiple variables into other areas of my Terraform configuration. – hitman126 Commented Feb 25 at 11:11
  • If that's the case, you could also just use terraform console to verify the contents of the local variable. If you need to dump the entire thing, you could do it by using the splat syntax, i.e, value = local.config.variables[*]. – Marko E Commented Feb 25 at 12:00
Add a comment  | 

1 Answer 1

Reset to default 2

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"

本文标签: azure pipelinesParsing YAML Variables and Parameters to Terraform Configuration FileStack Overflow