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 definitionlocals.tf
- parse myazure-pipelines.yaml
file withyamldecode
and convert to a Terraform-compatible representationoutputs.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 definitionlocals.tf
- parse myazure-pipelines.yaml
file withyamldecode
and convert to a Terraform-compatible representationoutputs.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
|
1 Answer
Reset to default 2You 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
版权声明:本文标题:azure pipelines - Parsing YAML Variables and Parameters to Terraform Configuration File - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741211896a2359288.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
value = [for var in local.config.variables : var.value if var.name == "ResourceGroup"][0]
. – Marko E Commented Feb 25 at 10:59terraform 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