如何在 terraform 中使用 json 文件
how to use json file in terraform
我有以下 JSON 文件并在 terraform 中创建用户时使用它
{
"SRE@abc.com" : [
{
"name" : "abc",
"description" : 100,
},
{
"name" : "efg",
"description" : 100
},
{
"name" : "wer",
"description" : 100
}
],
"SDE@abc.com" : [
{
"name" : "xyz",
"description" : 100
},
{
"name" : "qwe",
"description" : 100
}
]
}
我尝试了一些东西,但在创建资源时出错了……。我不知道如何获取键、值
resource "oci_identity_user" "users" {
count = keys(local.users)[*]
compartment_id = var.tenancy_ocid
description = count.index[description]
name = count.index[name]
email = count.index[email]
}
Error: Invalid index
│
│ on local.tf line 14, in locals:
│ 14: services = keys(local.users[1])
│ ├────────────────
│ │ local.odsc_serivices is object with 2 attributes
│
由于您的 users
对象中有两个列表,因此您必须将它们连接起来。您可以通过组合 flatten
and values
函数来做到这一点。此外,我建议使用 for_each
而不是 count
:
resource "oci_identity_user" "users" {
for_each = { for index, value in flatten(values(local.users)) : index => value }
compartment_id = var.tenancy_ocid
description = each.value.name
name = each.value.description
email = each.value.email
}
更新:
resource "oci_identity_user" "users" {
for_each = {
for index, value in flatten([
for email, value in local.users : [
for user in value : { email : email, name : user.name, description : user.description }
]
]) : index => value
}
compartment_id = var.tenancy_ocid
description = each.value.name
name = each.value.description
email = each.value.email
}
我有以下 JSON 文件并在 terraform 中创建用户时使用它
{
"SRE@abc.com" : [
{
"name" : "abc",
"description" : 100,
},
{
"name" : "efg",
"description" : 100
},
{
"name" : "wer",
"description" : 100
}
],
"SDE@abc.com" : [
{
"name" : "xyz",
"description" : 100
},
{
"name" : "qwe",
"description" : 100
}
]
}
我尝试了一些东西,但在创建资源时出错了……。我不知道如何获取键、值
resource "oci_identity_user" "users" {
count = keys(local.users)[*]
compartment_id = var.tenancy_ocid
description = count.index[description]
name = count.index[name]
email = count.index[email]
}
Error: Invalid index
│
│ on local.tf line 14, in locals:
│ 14: services = keys(local.users[1])
│ ├────────────────
│ │ local.odsc_serivices is object with 2 attributes
│
由于您的 users
对象中有两个列表,因此您必须将它们连接起来。您可以通过组合 flatten
and values
函数来做到这一点。此外,我建议使用 for_each
而不是 count
:
resource "oci_identity_user" "users" {
for_each = { for index, value in flatten(values(local.users)) : index => value }
compartment_id = var.tenancy_ocid
description = each.value.name
name = each.value.description
email = each.value.email
}
更新:
resource "oci_identity_user" "users" {
for_each = {
for index, value in flatten([
for email, value in local.users : [
for user in value : { email : email, name : user.name, description : user.description }
]
]) : index => value
}
compartment_id = var.tenancy_ocid
description = each.value.name
name = each.value.description
email = each.value.email
}