terraform是否支持使用另一个json文件的查找结果从json文件中查找值?
我有一个特定于环境的json文件,例如prod.json,如下所示:terraform是否支持使用另一个json文件的查找结果从json文件中查找值?,terraform,Terraform,我有一个特定于环境的json文件,例如prod.json,如下所示: { "subscription": "xxxxxx", "tshirt_size": "large", "env_prefix": "prod" { "tshirtsizes": { "small": { "vmcount"
{
"subscription": "xxxxxx",
"tshirt_size": "large",
"env_prefix": "prod"
{
"tshirtsizes": {
"small": {
"vmcount": 1,
"cpucount": 2,
"memory": 4098
},
"medium": {
"vmcount": 2,
"cpucount": 2,
"memory": 8196
},
"large": {
"vmcount": 4,
"cpucount": 2,
"memory": 8196
}
}
}
我还有另一个global.json文件,如下所示:
{
"subscription": "xxxxxx",
"tshirt_size": "large",
"env_prefix": "prod"
{
"tshirtsizes": {
"small": {
"vmcount": 1,
"cpucount": 2,
"memory": 4098
},
"medium": {
"vmcount": 2,
"cpucount": 2,
"memory": 8196
},
"large": {
"vmcount": 4,
"cpucount": 2,
"memory": 8196
}
}
}
我的terraform配置需要知道创建新vm资源时要使用的cpu数量。我想使用prod.json中的'tshirt size'值('large')从global.json中查找适当的'cpucount'值。例如,如果我尝试以下方法:
locals {
tshirtsize = jsondecode(file("prod.json")).tshirt_size
cpucount = jsondecode(file("global.json")).tshirtsizes.${tshirtsize}.cpucount
}
我得到以下错误:
Error: Invalid character
on main.tf line 30, in locals:
30: cpucount = jsondecode(file("global.json")).tshirtsizes${tshirtsize}.cpucount
This character is not used within the language
Error: Invalid attribute name
on main.tf line 30, in locals:
30: cpucount = jsondecode(file("global.json")).tshirtsizes${tshirtsize}.cpucount
An attribute name is required after a dot.
显然,不支持像这样尝试使用插值(在资源本身中也尝试过)。有人知道如何做到这一点吗?
谢谢您正在做的
tshirtsize.${tshirtsize}.cpucount
这是无效的语法。以下是您的操作方法:
variable "prod" {
default = <<EOF
{
"subscription": "xxxxxx",
"tshirt_size": "large",
"env_prefix": "prod"
}
EOF
}
variable "global" {
default = <<EOF
{
"tshirtsizes": {
"small": { "cpucount": 1 },
"medium": { "cpucount": 2 },
"large": { "cpucount": 3 }
}
}
EOF
}
locals {
tshirtsize = jsondecode(var.prod).tshirt_size
cpucount = jsondecode(var.global).tshirtsizes[local.tshirtsize].cpucount
}
output "cpucount" {
value = local.cpucount
}
变量“prod”{
default=您正在执行的操作tshirtsize.${tshirtsize}.cpucount
这是无效语法。
以下是您的操作方法:
variable "prod" {
default = <<EOF
{
"subscription": "xxxxxx",
"tshirt_size": "large",
"env_prefix": "prod"
}
EOF
}
variable "global" {
default = <<EOF
{
"tshirtsizes": {
"small": { "cpucount": 1 },
"medium": { "cpucount": 2 },
"large": { "cpucount": 3 }
}
}
EOF
}
locals {
tshirtsize = jsondecode(var.prod).tshirt_size
cpucount = jsondecode(var.global).tshirtsizes[local.tshirtsize].cpucount
}
output "cpucount" {
value = local.cpucount
}
变量“prod”{
default=如果这些文件是输入变量文件,可能会容易得多。如果这些文件是输入变量文件,可能会容易得多。