如何在Terraform中从JSON引用数据

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

how to reference data from json in terraform

问题

以下是翻译好的内容:

我有一个包含IP地址的JSON文件它的结构如下所示我正在使用Terraform中的jsondecode来导入该文件希望能够使用其中的值我可以使用"US"和"ALL"值而没有问题。我的问题是当我尝试引用"Regions"下的值时请参见下面的内容

```json
{
   "US": [
        "30.30.30.85",
        "30.30.30.86"
    ],
    "All": [
        "30.30.30.85",
        "30.30.30.86",
        "40.40.40.40",
        "40.40.40.41",
        "50.50.50.33",
        "50.50.50.34",
        "60.60.60.61",
        "60.60.60.62"
    ],
    "Regions": [
        {
            "US_Central": [
                "40.40.40.40",
                "40.40.40.41"
            ]
        },
        {
            "US_Southeast": [
                "50.50.50.33",
                "50.50.50.34"
            ]
        },
        {
            "UK": [
                "60.60.60.61",
                "60.60.60.62"
            ]
        }
    }
}
locals {
  ip_list = jsondecode(file("${path.module}/../ip_list.json"))
}

变量"ips"会出现各种错误,具体取决于如何调用它。到目前为止,我尝试了以下方式:

ips = local.ip_list.Regions.US_Central
ips = local.ip_list.Regions[*].US_Central

有谁知道引用这些值的正确方式吗?


<details>
<summary>英文:</summary>

I have a json file with IPs in it. It&#39;s structured like the example below. I&#39;m using jsondecode in terraform to ingest the file and i&#39;m hoping to use the values in it. I&#39;m able to use the &quot;US&quot; and &quot;ALL&quot; values without issue. The problem i have is when i try to reference the values under region. Please see below:

```json
    {
       &quot;US&quot;: [
            &quot;30.30.30.85&quot;,
            &quot;30.30.30.86&quot;
        ],
        &quot;All&quot;: [
            &quot;30.30.30.85&quot;,
            &quot;30.30.30.86&quot;,
            &quot;40.40.40.40&quot;,
    	    &quot;40.40.40.41&quot;,
    	    &quot;50.50.50.33&quot;,
    	    &quot;50.50.50.34&quot;,
    	    &quot;60.60.60.61&quot;,
    	    &quot;60.60.60.62&quot;
        ],
        &quot;Regions&quot;: [
            {
                &quot;US_Central&quot;: [
                    &quot;40.40.40.40&quot;,
    		        &quot;40.40.40.41&quot;
                ]
            },
            {
                &quot;US_Southeast&quot;: [
                    &quot;50.50.50.33&quot;,
    		        &quot;50.50.50.34&quot;
                ]
            },
            {
                &quot;UK&quot;: [
                    &quot;60.60.60.61&quot;,
    		        &quot;60.60.60.62&quot;
                ]
            }
        ]
    }


```hcl
locals {
  ip_list           = jsondecode(file(&quot;${path.module}/../ip_list.json&quot;))
}

The ips variable fails with various errors depending on how i try to call it. Here are the ways i've tried so far:

ips               = local.ip_list.Regions[US_Central].value
ips               = local.ip_list.Regions.US_Central
ips               = local.ip_list.Regions[*].US_Central

Does anyone know what the proper way to reference these values is?

答案1

得分: 1

自从`"Regions"`键是一个列表`[`表示),您可以使用索引来获取列表的第一个元素例如

```hcl
ips = local.ip_list.Regions[0].US_Central

terraform console中测试会得到:

> local.ips
[
  "40.40.40.40",
  "40.40.40.41",
]

然而,如果您想获取所有不同键(即US_CentralUS_SoutheastUK)的所有值,代码就需要进行调整。

编辑:我很可能没有以最优雅的方式来做这个,但以下是如何将所有IP的值获取到一个列表中:

ip_lists = values(merge(flatten([
    for i, region in local.ip_list.Regions : [
      for r, cidrs in region : {
        "${i}.cidr_block" = cidrs
      }
    ]
  ])...))

这将返回一个包含所有区域CIDR的列表列表,然后唯一剩下的就是再次将其“扁平化”:

flattened_list_of_ips = flatten(local.ip_lists)

返回:

> flatten(local.ip_lists)
[
  "40.40.40.40",
  "40.40.40.41",
  "50.50.50.33",
  "50.50.50.34",
  "60.60.60.61",
  "60.60.60.62",
]

<details>
<summary>英文:</summary>

Since `&quot;Regions&quot;` key is a list (denoted with `[`), you can fetch the first element of the list by using an index, e.g:

```hcl
ips = local.ip_list.Regions[0].US_Central

Testing with terraform console gives:

&gt; local.ips
[
  &quot;40.40.40.40&quot;,
  &quot;40.40.40.41&quot;,
]

However, if you want to fetch all the values for all the different keys (i.e., US_Central, US_Southeast, and UK), the code would have to be adjusted.

EDIT: I am most probably not doing this in the most elegant way, but here is how you can get all the values of IPs in one list:

ip_lists = values(merge(flatten([
    for i, region in local.ip_list.Regions : [
      for r, cidrs in region : {
        &quot;${i}.cidr_block&quot; = cidrs
      }
    ]
  ])...))

This will return a list of lists with CIDRs in all regions, and then the only thing left is to flatten it again:

flattened_list_of_ips = flatten(local.ip_lists)

which returns:

&gt; flatten(local.ip_lists)
[
  &quot;40.40.40.40&quot;,
  &quot;40.40.40.41&quot;,
  &quot;50.50.50.33&quot;,
  &quot;50.50.50.34&quot;,
  &quot;60.60.60.61&quot;,
  &quot;60.60.60.62&quot;,
]

huangapple
  • 本文由 发表于 2023年2月23日 22:55:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546498.html
匿名

发表评论

匿名网友

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

确定