我需要插值语法来分配变量给资源参数吗?

huangapple go评论51阅读模式
英文:

Do I need interpolation syntax when assigning a variable to a resource argument?

问题

在Terraform文档中,有时我会遇到类似于以下示例的情况,它们使用插值语法("${...}")将变量分配给资源参数:

resource "vsphere_folder" "folder" {
  path          = "terraform-test-folder"
  type          = "vm"
  datacenter_id = "${data.vsphere_datacenter.dc.id}"
}

而有时我会遇到不使用插值语法的示例,如以下示例:

resource "vsphere_virtual_disk" "virtual_disk" {
  size               = 40
  type               = "thin"
  vmdk_path          = "/foo/foo.vmdk"
  create_directories = true
  datacenter         = data.vsphere_datacenter.datacenter.name
  datastore          = data.vsphere_datastore.datastore.name
}

在我的测试中,我无法为需要插值语法的变量(带有空格和特殊字符)创建内容。那么,我是否真的需要它?它是否以任何方式增强了我的代码的稳健性?

英文:

In the Terraform docs, I sometimes stumble upon examples like this:

> terraform
> resource "vsphere_folder" "folder" {
> path = "terraform-test-folder"
> type = "vm"
> datacenter_id = "${data.vsphere_datacenter.dc.id}"
> }
>

where they use interpolation syntax ("${...}") to assign a variable to a resource argument and sometimes I stumble upon examples like this:

> terraform
> resource "vsphere_virtual_disk" "virtual_disk" {
> size = 40
> type = "thin"
> vmdk_path = "/foo/foo.vmdk"
> create_directories = true
> datacenter = data.vsphere_datacenter.datacenter.name
> datastore = data.vsphere_datastore.datastore.name
> }
>

where they do not use interpolation syntax.

In my testing, I was not able to craft content for a variable (with spaces and special chars) that needed the interpolation syntax. So, do I need it at all? Does it make my code more robust in any way?

答案1

得分: 3

在2019年5月19日,Terraform 0.12发布,具备了一流的表达式语法。这对应于从HCL到HCL2的更新。在此更新之前,需要使用字符串内插语法。任何文档中仍然使用字符串内插语法的示例基本上意味着该文档在四年多的时间内未进行更新。

请注意,从版本0.14开始,使用不必要的字符串内插语法开始发出警告,并在版本0.15中通过terraform fmt自动更正为一流表达式。

英文:

On May 19, 2019 Terraform 0.12 was released with first class expression syntax. This corresponds to the update from HCL to HCL2. Prior to that update string interpolation syntax was required. Any examples in documentation that still use the string interpolation syntax essentially imply that the documentation has not been updated in more than four years.

Note that usage of string interpolation syntax where it was not required began emitting warnings in version 0.14, and was auto-corrected to a first class expression with terraform fmt beginning in version 0.15.

huangapple
  • 本文由 发表于 2023年6月26日 18:14:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定