Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables 如何使用terrafom创建文件并将变量包含为文字?_Variables_Ansible_Interpolation_Terraform_Postfix Mta - Fatal编程技术网

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

Variables 如何使用terrafom创建文件并将变量包含为文字?,variables,ansible,interpolation,terraform,postfix-mta,Variables,Ansible,Interpolation,Terraform,Postfix Mta,使用terraform v0.12.9并使用template_file数据源构建文件,我不能使用双美元符号$$将输入${data_directory}视为文本 正在寻找以正确方式解决此问题的解决方案,或者寻找任何其他建议或解决方法,以帮助创建包含此内容的文件 我尝试使用双美元符号(如下面的代码示例中)将此${data_directory}隔离为文件输出中的文本 下面是我试图使用terraform创建postfix main.cf文件的代码: variable "hostname" { def

使用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}"
    }

}

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

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

##
## 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
Error: failed to render : <template_file>:11,43-57: Unknown variable; There is no variable named "data_directory".
错误:未能呈现::11,43-57:未知变量;没有名为“数据目录”的变量。

模板文件
在Terraform中仍然可用,主要用于Terraform 0.11用户。在Terraform 0.12中,无需使用
模板文件
,因为它已被其他两个功能所取代:

  • 对于单独文件中的模板,可以直接用该语言呈现外部模板,而无需单独的提供程序和数据源
  • 对于内联模板(直接在配置中指定),您可以直接将它们写入需要的位置,或者通过将它们分解出来
与使用
local exec
provisioner相比,
local_file
资源也是在磁盘上创建本地文件的更好方法。通过使用
template\u文件
local exec
,您将不得不应付两个级别的额外转义:Terraform模板转义以将文本模板放入
template\u文件
数据源,然后在Provisionier内部进行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
}
变量“后缀配置路径”{
#注意,对于我的示例,这应该是完整路径
#对于文件,而不仅仅是文件名。Terraform习惯用法是
#对这类事情的明确解释,而不是依赖
#家庭等环境变量。
类型=字符串
}
当地人{

postfix_config=谢谢Martin,非常好的解释和如何正确使用Terraform的指南。:-)还感谢您对AWS SSM参数存储和HashiCorp Consor的解释,我不知道这些服务。我非常感谢。