英文:
Failed to extract specific value from map object terraform
问题
这是我的主文件
services_list = flatten([
reverse(values(var.host_svc)) ])
}
variable "host_svc" {
type = map(map(any))
}
output "aa" {
value = local.services_list
}
这是Terraform的tfvars文件
host_svc = {
accounts = {
name = "accounts";
pod = "3";
cpu = "512M";
memory = "1024M";
}
analytics = {
name = "analytics";
pod = "3";
cpu = "512M";
memory = "1024M";
}
}
当我输出local.services_list时,我得到整个对象。我只想提取每个映射对象的名称,例如accounts、analytics。
我尝试过使用列表数据类型,它可以工作,但我想使用映射对象,因为我需要使用不同的对象值。
英文:
This is my main file
services_list = flatten([
reverse(values(var.host_svc)) ])
}
variable "host_svc" {
type = map(map(any))
}
output "aa" {
value = local.services_list
}
This is terraform tfvars file
host_svc = {
accounts = {
name = "accounts"
pod = "3"
cpu = "512M"
memory = "1024M"
}
analytics = {
name = "analytics"
pod = "3"
cpu = "512M"
memory = "1024M"
}
}
When i am output local.services_list i ma getting whole object. i just want to fetch just name of each map object like accounts,analytics.
i have tried with list data type it works but i want to do with map ojject because i have to use different object values
答案1
得分: 1
你可以比你现在尝试的更容易地完成这个任务:
local {
services_list = [for k, v in var.host_svc : k]
}
或者更简单地使用内置的 keys
函数 1:
local {
services_list = keys(var.host_svc)
}
1 https://developer.hashicorp.com/terraform/language/functions/keys
英文:
You can do that much easier than you are trying:
local {
services_list = [for k, v in var.host_svc : k]
}
Or even more easy using the built-in keys
function 1:
local {
services_list = keys(var.host_svc)
}
1 https://developer.hashicorp.com/terraform/language/functions/keys
答案2
得分: 1
如果我理解正确,您想从您的映射中读取名称。如果您想读取您的映射键,只需使用keys(map)
函数:
variable "host_svc" {
type = map(map(any))
default = {
accounts_k = {
name = "accounts";
pod = "3";
cpu = "512M";
memory = "1024M";
}
analytics_k = {
name = "analytics";
pod = "3";
cpu = "512M";
memory = "1024M";
}
}
}
output "aa" {
value = keys(var.host_svc)
}
这将输出:
+ aa = [
+ "accounts_k",
+ "analytics_k",
]
我只是使用了_k
后缀,以区分映射键和内部映射中的name
键
如果您想读取该映射的name
属性,可以这样做:
output "aa" {
value = [for svc in local.services_list : svc.name]
}
如果您不想在输出的列表中有重复项,请使用toset()
:
output "aa" {
value = toset([for svc in local.services_list : svc.name])
}
英文:
If I understood you correctly, you want to read the names from your map. If you want to read your map keys, just use keys(map)
function:
variable "host_svc" {
type = map(map(any))
default = {
accounts_k = {
name = "accounts"
pod = "3"
cpu = "512M"
memory = "1024M"
}
analytics_k = {
name = "analytics"
pod = "3"
cpu = "512M"
memory = "1024M"
}
}
}
output "aa" {
value = keys(var.host_svc)
}
Which will output:
+ aa = [
+ "accounts_k",
+ "analytics_k",
]
I used _k
suffix just to differentiate between the map keys and your name
key inside the internal map
In case you want to read name
attribute of that map you can do:
output "aa" {
value = [for svc in local.services_list : svc.name]
}
In case you don't want to have duplicates in that outputted list, use toset()
:
output "aa" {
value = toset([for svc in local.services_list : svc.name])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论