0

我需要根据调用资源时最终使用哪个变量(来自一组始终存在的预定义变量)来呈现模板。

例子:

变量.tf

possible_choice    = "this" 
another_choice     = "that"
yet_another_choice = "then"

主文件

resource "instance" "this_name" {
image_id = var.possible_choice
user_data   = templatefile( "my_template_file.tpl", {choice = image_id})
}

模板文件:

%{ if choice == ....) %{endif}

我无法完成的是传递选择在资源中实现的变量的值。

  • 我无法检查变量是否为空(因为它们都在 variables.tf 中定义并且将返回一个字符串)。

  • 我不能通过instance.this_name,因为命名不统一(可能是 ofthis_name.possible_choice等等)。

如果可以提取其中包含的任何默认值instance.this_name.image_id,那么我应该很高兴,我想。我尝试了多种方法,但大多数都得到了cannot refer to itself,这是有道理的。

谢谢你。

4

1 回答 1

0

根据您的要求,为什么不将选择隐式移动到它自己的变量中呢?

possible_choice    = "this" 
another_choice     = "that"
yet_another_choice = "then"
actual_choice      = var.WHATEVER_CHOICE_IT_IS

resource "instance" "this_name" {
image_id = var.actual_choice
user_data   = templatefile( "my_template_file.tpl", {choice = var.actual_choice})
}

您只是在其自己的变量中跟踪实际选择并将其传递给资源本身。

于 2021-01-09T23:01:08.307 回答