如何使用 terrafom 创建文件并将变量包含为文字?

hob*_*bak 8 variables interpolation postfix-mta ansible terraform

使用 terraform v0.12.9 并使用 template_file 数据源构建文件,我无法使用双美元符号$$将输入${data_directory}视为文字。

寻找解决方案以正确的方式解决此问题,或寻找任何其他建议或解决方法来帮助创建包含此内容的文件。

我尝试使用双美元符号(如下面的代码示例所示)将此${data_directory}隔离为文件输出中的文字。

这是我尝试使用 terraform 创建 postfix main.cf 文件的代码:

variable "hostname" {
  default = "test"
}

variable "domain_name" {
  default = "test.com"
}

variable "fn_main_cf" {
  default = "main.cf"
}

data "template_file" "main_cf" {
  template = <<EOF
##
## Network settings
##
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
inet_interfaces = 127.0.0.1, ::1, 120.121.123.124, 2a03:b0a0:3:d0::5e79:4001
myhostname = ${var.hostname}.${var.domain_name}

###
### Outbound SMTP connections (Postfix as sender)
###
smtp_tls_session_cache_database = btree:$${data_directory}/smtp_scache
EOF
}

  data "template_cloudinit_config" "main_cf" {
    gzip          = false
    base64_encode = false

    part {
      filename     = "${var.fn_main_cf}"
      content_type = "text/cloud-config"
      content      = "${data.template_file.main_cf.rendered}"
    }
  }

  resource "null_resource" "main_cf" {
    triggers = {
      template = "${data.template_file.main_cf.rendered}"
    }

    provisioner "local-exec" {
      command = "echo \"${data.template_file.main_cf.rendered}\" > ~/projects/mail-server/files/etc/postfix/${var.fn_main_cf}"
    }

}

Run Code Online (Sandbox Code Playgroud)

正如您所看到的,有很多变量,并且所有这些都工作正常,但是 ${data_directory} 不应被视为变量,而应被视为文字,并且应保留在磁盘上的输出文件中。

保存在磁盘上的main.cf创建的文件中的预期输出应如下所示:

##
## Network settings
##
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
inet_interfaces = 127.0.0.1, ::1, 120.121.123.124, 2a03:b0a0:3:d0::5e79:4001 
myhostname = test.test.com

###
### Outbound SMTP connections (Postfix as sender)
###
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
Run Code Online (Sandbox Code Playgroud)

因此${data_directory}不应被 terraform 视为 terraform 变量,而应视为一组字符、文字(常规文本输入)。

运行terraform plan带有双美元符号$$的输出如下:

Error: failed to render : <template_file>:11,43-57: Unknown variable; There is no variable named "data_directory".
Run Code Online (Sandbox Code Playgroud)

Mar*_*ins 9

template_fileTerraform 中仍然主要供 Terraform 0.11 用户使用。在 Terraform 0.12 中,不需要使用template_file,因为它已被其他两个功能取代:

  • 对于单独文件中的模板,内置templatefile函数可以直接在语言中呈现外部模板,而不需要单独的提供程序和数据源。
  • 对于内联模板(直接在配置中指定),您可以直接将它们写入需要的位置,或者通过Local Values将它们分解出来。

与使用配置程序相比,该local_file资源也是在磁盘上创建本地文件的更好方法local-exec。通过使用template_fileand local-exec,您将强迫自己应对两个级别的额外转义:Terraform 模板转义以将文字模板转义到数据template_file源中,然后在您的配置程序中进行 shell 转义。

这是表示模板和文件的更直接的方法:

variable "postfix_config_path" {
  # Note that for my example this is expected to be the full path
  # to the file, not just the filename. Terraform idiom is to be
  # explicit about this sort of thing, rather than relying on
  # environment variables like HOME.
  type = string
}

locals {
  postfix_config = <<-EOT
    ##
    ## Network settings
    ##
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    inet_interfaces = 127.0.0.1, ::1, 120.121.123.124, 2a03:b0a0:3:d0::5e79:4001
    myhostname = ${var.hostname}.${var.domain_name}

    ###
    ### Outbound SMTP connections (Postfix as sender)
    ###
    smtp_tls_session_cache_database = btree:$${data_directory}/smtp_scache
  EOT
}

resource "local_file" "postfix_config" {
  filename = var.postfix_config_path
  content  = local.postfix_config
}
Run Code Online (Sandbox Code Playgroud)

正如local提供程序文档所警告的那样,Terraform 并不是真正为直接管理本地计算机上的文件和其他资源而设计的。提供商会local处理异常情况,这可能是其中一种情况,在这种情况下,上述方法是解决该问题的合理方法。

请注意,更标准的 Terraform 使用模式是使用 Terraform 启动一个新的虚拟机,该虚拟机将运行 Postfix 并通过特定于供应商的参数user_datametadata参数传递必要的配置。

如果 postfix 服务器与此 Terraform 配置分开管理,则另一种模式是安排 Terraform 将必要的数据写入共享配置存储(例如 AWS SSM Parameter Store 或 HashiCorp Consul),然后在 postfix 上使用单独的软件服务器读取该文件并更新该main.cf文件。对于 HashiCorp Consul,该单独的软件可能是consul-template。其他参数存储也存在类似的软件,允许您将单个虚拟机的配置与整个基础架构的配置分离。