azure - Processing Terraform output to give a list
问题描述
I'm looking for a way to pull specific values out of a complex data structure in my Terraform output. The output looks like this:
user_assigned_identities = [
{
"storage-contributor" = {
"client_id" = "xxxx-xxxxx-xxxxx-xxxxx"
"id" = "/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-1"
"location" = "uksouth"
"name" = "test-identity-1"
"principal_id" = "xxxx-xxxxx-xxxxx-xxxxx"
"resource_group_name" = "rg-mgmt-jenkins"
"tags" = {}
}
"test-identity" = {
"client_id" = "187806c3-7676-4869-abb5-0121de175780"
"id" = "/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-2"
"location" = "uksouth"
"name" = "test-identity-2"
"principal_id" = "xxxx-xxxxx-xxxxx-xxxxx"
"resource_group_name" = "rg-mgmt-jenkins"
"tags" = {}
}
},
]
I would like to get output like:
identity_ids = [
"/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-1"
"/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-2"
]
...using local{}
which I can then feed into the identity
block of the VM or AKS configuration. I can't work out, though, how to do that using Terraform functions and expressions. I have managed it with jq
:
[map(.[]) | .[].id]
解决方案
假设您总是有一个只有 1 个元素的列表,然后将不同的身份作为映射中的键,那么您可以在列表中的第一个元素上使用for
表达式,如下所示:
locals {
user_assigned_identities = [
{
"storage-contributor" = {
"client_id" = "xxxx-xxxxx-xxxxx-xxxxx"
"id" = "/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-1"
"location" = "uksouth"
"name" = "test-identity-1"
"principal_id" = "xxxx-xxxxx-xxxxx-xxxxx"
"resource_group_name" = "rg-mgmt-jenkins"
"tags" = {}
}
"test-identity" = {
"client_id" = "187806c3-7676-4869-abb5-0121de175780"
"id" = "/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-2"
"location" = "uksouth"
"name" = "test-identity-2"
"principal_id" = "xxxx-xxxxx-xxxxx-xxxxx"
"resource_group_name" = "rg-mgmt-jenkins"
"tags" = {}
}
},
]
}
output "identity_ids" {
value = [ for identity in local.user_assigned_identities[0] : identity.id ]
}
输出以下内容:
identity_ids = [
"/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-1",
"/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourcegroups/rg-mgmt-jenkins/providers/Microsoft.ManagedIdentity/userAssignedIdentities/test-identity-2",
]
推荐阅读
- php - laravel 显示单元测试没有断言
- sdn - 如何使用 ryu 控制器向所有交换机发送数据包?
- javascript - Firebase Admin SDK - 错误:提供给 initializeApp() 的凭据实现...未能获取有效的 Google OAuth2 访问令牌
- c++ - GTKmm Opengl 上下文未初始化
- c# - Newtonsoft.Json 未按预期序列化对象 - 错误 400
- laravel - laravel形式的坐标输入类型collective
- python - 运行 python 文件后,导入的模块未导入
- c# - 连接到在 VM 上运行的 MongoDB
- java - 获取两天之间的差异(减法)
- c# - Raspberry PI - I2C 不写入数据