我需要根据在调用资源时最终使用哪个变量(来自一组始终存在的预定义变量)来呈现模板。
示例:
变量.tf
possible_choice = "this"
another_choice = "that"
yet_another_choice = "then"
main.tf
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
,因为命名不统一(可能是 this_name.possible_choice
等等)。
如果可以提取 instance.this_name.image_id
中包含的任何默认值,那么我应该很高兴,我想。我尝试了多种方法,但大部分都得到了 cannot refer to itself
,这是有道理的。
谢谢。
答案 0 :(得分: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})
}
您只是在其自己的变量中跟踪实际选择并将其传递给资源本身。