英文:
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's structured like the example below. I'm using jsondecode in terraform to ingest the file and i'm hoping to use the values in it. I'm able to use the "US" and "ALL" values without issue. The problem i have is when i try to reference the values under region. Please see below:
```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"
]
}
]
}
```hcl
locals {
ip_list = jsondecode(file("${path.module}/../ip_list.json"))
}
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_Central
,US_Southeast
和UK
)的所有值,代码就需要进行调整。
编辑:我很可能没有以最优雅的方式来做这个,但以下是如何将所有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 `"Regions"` 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:
> local.ips
[
"40.40.40.40",
"40.40.40.41",
]
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 : {
"${i}.cidr_block" = 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:
> 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",
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论