Terraform 条件变量

let*_*tsc 3 amazon-web-services terraform

我的 terraform 配置中有一个模板,我在其中写入变量的值,如下所示:

data "template_file" "config" {
  template = "${file("${path.module}/templates/${var.json_config}")}"

  vars {
    is_enabled = "${var.is_enabled}"
  }
}
Run Code Online (Sandbox Code Playgroud)

Nowis_enabled是一个布尔字符串,设置为truefalse。现在,根据这是真还是假,我想设置另一个变量。在伪代码中它看起来像这样:

如果 is_enabled == true 路径 = /一个/路径/ 否则路径 = /另一个/路径

我看过了,conditional values但似乎是为了提供资源。我将如何使用它在模板文件中设置变量?

Hau*_*eth 5

模板使用与 Terraform 中所有其他字符串相同的插值语法。文档可用

所以在你的情况下它看起来像这样:

path = ${is_enabled ? "/one/path/" : "/another/path"}
Run Code Online (Sandbox Code Playgroud)