Does terraform support math rounding?

China☆狼群 提交于 2019-12-13 00:42:04

问题


Is it possible to round an integer value in terraform string interpolations?


回答1:


It's a bit of a hack and doesn't use terraform string interpolations but..

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.

Terraform:

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}'

Here is an issue about supporting "round" in terraform: https://github.com/hashicorp/terraform/issues/16251



来源:https://stackoverflow.com/questions/53766675/does-terraform-support-math-rounding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!