英文:
How do I select just a subset of Terraform Outputs from the following structure
问题
Here's the translated code portion:
我正在使用Terraform,并且有以下输出。
理想情况下,我只想获取这些信息的子集 - 具体来说是`ip_address`。这是我的当前`outputs.tf`看起来像这样。
但是当我尝试以下操作时,会出现错误:
正确的解决方案是什么?
<details>
<summary>英文:</summary>
I am using Terraform with the following output.
```json
ip_addresses = [
{
"northcentralus" = {
"allocation_method" = "Dynamic"
"ddos_protection_mode" = "VirtualNetworkInherited"
"ddos_protection_plan_id" = tostring(null)
"domain_name_label" = ""
"fqdn" = ""
"id" = "/subscriptions/SUBID/resourceGroups/multisqlite-bacalhau-example-northcentralus-rg/providers/Microsoft.Network/publicIPAddresses/multisqlite-bacalhau-example-northcentralus-public-ip"
"idle_timeout_in_minutes" = 4
"ip_address" = "1.2.3.4"
"ip_tags" = tomap({})
"ip_version" = "IPv4"
"location" = "northcentralus"
"name" = "multisqlite-bacalhau-example-northcentralus-public-ip"
"resource_group_name" = "multisqlite-bacalhau-example-northcentralus-rg"
"reverse_fqdn" = ""
"sku" = "Basic"
"tags" = tomap({})
"timeouts" = null /* object */
"zones" = tolist([])
}
"southafricanorth" = {
"allocation_method" = "Dynamic"
"ddos_protection_mode" = "VirtualNetworkInherited"
"ddos_protection_plan_id" = tostring(null)
"domain_name_label" = ""
"fqdn" = ""
"id" = "/subscriptions/SUBID/resourceGroups/multisqlite-bacalhau-example-southafricanorth-rg/providers/Microsoft.Network/publicIPAddresses/multisqlite-bacalhau-example-southafricanorth-public-ip"
"idle_timeout_in_minutes" = 4
"ip_address" = "1.2.3.5"
"ip_tags" = tomap({})
"ip_version" = "IPv4"
"location" = "southafricanorth"
"name" = "multisqlite-bacalhau-example-southafricanorth-public-ip"
"resource_group_name" = "multisqlite-bacalhau-example-southafricanorth-rg"
"reverse_fqdn" = ""
"sku" = "Basic"
"tags" = tomap({})
"timeouts" = null /* object */
"zones" = tolist([])
}
},
]
Ideally, I'd love just to get a subset of this information - specifically the ip_address
. This is what my current outputs.tf
looks like.
output "ip_addresses" {
value = data.azurerm_public_ip.public_ip.*
}
But when I try the following, I get errors:
output "ip_addresses" {
value = data.azurerm_public_ip.public_ip[*].ip_address
}
What's the right solution here?
答案1
得分: 3
尽管问题中没有显示,但我将根据问题中的信息假设data
包含一个具有map
类型的for_each
元参数,其中键是示例输出中显示的两个键,用于list(map)
。在这种假设下,可以使用output
中的for
表达式组装IP地址:
output "ip_addresses" {
value = { for region, attributes in data.azurerm_public_ip.public_ip : region => attributes.ip_address }
}
output
中的结果映射将如下所示:
{
"northcentralus" = "1.2.3.4",
"southafricanorth" = "1.2.3.5"
}
这是我根据问题推断出的期望结果结构。如果您希望得到的是list(string)
的IP地址列表:
value = [ for region, attributes in data.azurerm_public_ip.public_ip : attributes.ip_address ]
# ["1.2.3.4", "1.2.3.5"]
如果期望的转换与这些不同,那么从提供的两个解决方案中仍然应该能够清晰理解。
英文:
Although not shown in the question I will assume based on the information in the question that the data
contains a for_each
meta-parameter with a map
type where the keys are the two keys displayed in the example output for the list(map)
. Given that assumption, one can assemble the ip addresses with a for
expression in the output
value
:
output "ip_addresses" {
value = { for region, attributes in data.azurerm_public_ip.public_ip : region => attributes.ip_address }
}
The resulting map in the output
would appear as:
{
"northcentralus" = "1.2.3.4",
"southafricanorth" = "1.2.3.5"
}
which is my best inference about the desired resultant structure based on the question. If instead you are looking for a list(string)
of the ip addresses:
value = [ for region, attributes in data.azurerm_public_ip.public_ip : attributes.ip_address ]
# ["1.2.3.4", "1.2.3.5"]
If the desired transformation is something other than these, then hopefully it should still be clear from the two solutions provided.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论