检查 terraform 中的空值
check null values in terraform
我对空值有疑问。以下是对我来说棘手的一个。谁能建议我如何检查该值是否为空?
locals {
application_vars = {
"oke_build" = [
"121",
"121",
"121",
]
"ipa" = [
"101.10.2,1",
"101.10.2,2",
null
]
"size" = [
"c4.8xlarge",
null,
null,
]
}
}
我想验证列表是否包含空值。
您可以使用 compact 函数从列表中获取 non-null 个元素。
然后,您可以将原始列表的长度与 compact
函数返回的列表的长度进行比较。
locals {
application_vars = {
"oke_build" = [
"121",
"121",
"121",
]
"ipa" = [
"101.10.2,1",
"101.10.2,2",
null
]
"size" = [
"c4.8xlarge",
null,
null,
]
}
}
output "not_null_check_oke_build" {
value = length(compact(local.application_vars.oke_build)) == length(local.application_vars.oke_build) ? "has no null elements" : "has null elements"
}
output "not_null_check_ipa" {
value = length(compact(local.application_vars.ipa)) == length(local.application_vars.ipa) ? "has no null elements" : "has null elements"
}
output "not_null_check_size" {
value = length(compact(local.application_vars.size)) == length(local.application_vars.size) ? "has no null elements" : "has null elements"
}
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
not_null_check_ipa = "has null elements"
not_null_check_oke_build = "has no null elements"
not_null_check_size = "has null elements"
如果您要检查的列表太多,这会变得重复,您必须将这些与另一个循环循环。但是对于 2-3 个列表,这样设置是可以的。
我对空值有疑问。以下是对我来说棘手的一个。谁能建议我如何检查该值是否为空?
locals {
application_vars = {
"oke_build" = [
"121",
"121",
"121",
]
"ipa" = [
"101.10.2,1",
"101.10.2,2",
null
]
"size" = [
"c4.8xlarge",
null,
null,
]
}
}
我想验证列表是否包含空值。
您可以使用 compact 函数从列表中获取 non-null 个元素。
然后,您可以将原始列表的长度与 compact
函数返回的列表的长度进行比较。
locals {
application_vars = {
"oke_build" = [
"121",
"121",
"121",
]
"ipa" = [
"101.10.2,1",
"101.10.2,2",
null
]
"size" = [
"c4.8xlarge",
null,
null,
]
}
}
output "not_null_check_oke_build" {
value = length(compact(local.application_vars.oke_build)) == length(local.application_vars.oke_build) ? "has no null elements" : "has null elements"
}
output "not_null_check_ipa" {
value = length(compact(local.application_vars.ipa)) == length(local.application_vars.ipa) ? "has no null elements" : "has null elements"
}
output "not_null_check_size" {
value = length(compact(local.application_vars.size)) == length(local.application_vars.size) ? "has no null elements" : "has null elements"
}
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
not_null_check_ipa = "has null elements"
not_null_check_oke_build = "has no null elements"
not_null_check_size = "has null elements"
如果您要检查的列表太多,这会变得重复,您必须将这些与另一个循环循环。但是对于 2-3 个列表,这样设置是可以的。