如何从以下结构中仅选择 Terraform 输出的子集

huangapple go评论62阅读模式
英文:

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 = [
  {
    &quot;northcentralus&quot; = {
      &quot;allocation_method&quot; = &quot;Dynamic&quot;
      &quot;ddos_protection_mode&quot; = &quot;VirtualNetworkInherited&quot;
      &quot;ddos_protection_plan_id&quot; = tostring(null)
      &quot;domain_name_label&quot; = &quot;&quot;
      &quot;fqdn&quot; = &quot;&quot;
      &quot;id&quot; = &quot;/subscriptions/SUBID/resourceGroups/multisqlite-bacalhau-example-northcentralus-rg/providers/Microsoft.Network/publicIPAddresses/multisqlite-bacalhau-example-northcentralus-public-ip&quot;
      &quot;idle_timeout_in_minutes&quot; = 4
      &quot;ip_address&quot; = &quot;1.2.3.4&quot;
      &quot;ip_tags&quot; = tomap({})
      &quot;ip_version&quot; = &quot;IPv4&quot;
      &quot;location&quot; = &quot;northcentralus&quot;
      &quot;name&quot; = &quot;multisqlite-bacalhau-example-northcentralus-public-ip&quot;
      &quot;resource_group_name&quot; = &quot;multisqlite-bacalhau-example-northcentralus-rg&quot;
      &quot;reverse_fqdn&quot; = &quot;&quot;
      &quot;sku&quot; = &quot;Basic&quot;
      &quot;tags&quot; = tomap({})
      &quot;timeouts&quot; = null /* object */
      &quot;zones&quot; = tolist([])
    }
    &quot;southafricanorth&quot; = {
      &quot;allocation_method&quot; = &quot;Dynamic&quot;
      &quot;ddos_protection_mode&quot; = &quot;VirtualNetworkInherited&quot;
      &quot;ddos_protection_plan_id&quot; = tostring(null)
      &quot;domain_name_label&quot; = &quot;&quot;
      &quot;fqdn&quot; = &quot;&quot;
      &quot;id&quot; = &quot;/subscriptions/SUBID/resourceGroups/multisqlite-bacalhau-example-southafricanorth-rg/providers/Microsoft.Network/publicIPAddresses/multisqlite-bacalhau-example-southafricanorth-public-ip&quot;
      &quot;idle_timeout_in_minutes&quot; = 4
      &quot;ip_address&quot; = &quot;1.2.3.5&quot;
      &quot;ip_tags&quot; = tomap({})
      &quot;ip_version&quot; = &quot;IPv4&quot;
      &quot;location&quot; = &quot;southafricanorth&quot;
      &quot;name&quot; = &quot;multisqlite-bacalhau-example-southafricanorth-public-ip&quot;
      &quot;resource_group_name&quot; = &quot;multisqlite-bacalhau-example-southafricanorth-rg&quot;
      &quot;reverse_fqdn&quot; = &quot;&quot;
      &quot;sku&quot; = &quot;Basic&quot;
      &quot;tags&quot; = tomap({})
      &quot;timeouts&quot; = null /* object */
      &quot;zones&quot; = 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 &quot;ip_addresses&quot; {
  value = data.azurerm_public_ip.public_ip.*
}

But when I try the following, I get errors:

output &quot;ip_addresses&quot; {
  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 &quot;ip_addresses&quot; {
  value = { for region, attributes in data.azurerm_public_ip.public_ip : region =&gt; attributes.ip_address }
}

The resulting map in the output would appear as:

{
  &quot;northcentralus&quot;   = &quot;1.2.3.4&quot;,
  &quot;southafricanorth&quot; = &quot;1.2.3.5&quot;
}

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 ]
# [&quot;1.2.3.4&quot;, &quot;1.2.3.5&quot;]

If the desired transformation is something other than these, then hopefully it should still be clear from the two solutions provided.

huangapple
  • 本文由 发表于 2023年6月8日 02:49:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定