terraform 是否支持数学四舍五入? [英] Does terraform support math rounding?
问题描述
是否可以在 terraform 字符串插值中舍入整数值?
Is it possible to round an integer value in terraform string interpolations?
推荐答案
这有点像 hack,不使用 terraform 字符串插值,但是..
It's a bit of a hack and doesn't use terraform string interpolations but..
您可以使用外部数据源 (https://www.terraform.io/docs/providers/external/data_source.html) 委托给另一个程序.我包含的示例使用了 bash 和 jq.但是,您可能无需 jq 就可以实现这一点.
You can do this with the external data source (https://www.terraform.io/docs/providers/external/data_source.html) by delegating it to another program. The example I've included uses bash and jq. However you could probably achieve this without jq.
地形:
data external "rounder" {
program = ["bash", "${path.module}/round.sh"]
query {
value="1.3"
}
}
output "round" {
value = "${data.external.rounder.result.value}"
}
round.sh:
#!/usr/bin/env bash
# Exit if any of the intermediate steps fail
set -e
eval "$(jq -r '@sh "VALUE=\(.value)"')"
ROUNDED=$(printf "%.0f\n" $VALUE)
jq -n --arg rounded "$ROUNDED" '{"value":$rounded}'
这里有一个关于在 terraform 中支持round"的问题:https://github.com/hashicorp/terraform/issues/16251
Here is an issue about supporting "round" in terraform: https://github.com/hashicorp/terraform/issues/16251
这篇关于terraform 是否支持数学四舍五入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!