Terraform:heredoc 锚点中的无效字符

Terraform: invalid characters in heredoc anchor

我正在尝试在我的 terraform 脚本的 provisioner "remote-exec" 块中使用多行字符串。然而,每当我使用文档和各种示例中概述的 EOT 语法时,我都会收到一个错误,抱怨有:invalid characters in heredoc anchor.

这是收到此错误的简单 provisioner "remote-exec" 示例(两种类型的 EOT 在分别尝试时都会收到此错误):

provisioner "remote-exec" {
  inline = [
    << EOT 
    echo hi 
    EOT,

    << EOT 
    echo \
    hi 
    EOT,
  ]
}

更新:这是有效的解决方案,如果您遇到此问题,请仔细阅读,因为 Terraform 在 EOF 方面非常挑剔:

provisioner "remote-exec" {
  inline = [<<EOF

   echo foo
   echo bar

  EOF
  ]
}

请注意,如果您想使用 EOF,您在 provisioner "remote-exec" 块中使用的所有命令都必须在 EOF 内。您不能同时拥有 EOF 和非 EOF 两者。

EOF 的第一行必须这样开始,<<EOF 之后这一行不能有空格,否则会报错 invalid characters in heredoc anchor:

  inline = [<<EOF

你的 EOF 必须像这样以 EOF]

相同的缩进结束
  EOF
  ]

here-document 结束分隔符的末尾有一个逗号 (,)。这是不允许的。

试试这个:

provisioner "remote-exec" {
  inline = [
    <<EOT 
    echo hi
EOT
    ,
    <<EOT 
    echo \
    hi 
EOT
    ,
  ]
}

我不知道该文件的语法要求,但此处文档的结束分隔符需要与其开头使用的词相匹配。

此外,通常(在 shell 中),分隔符需要排在第一位(前面没有空格)。

事实上,Terraform documentation 是这样说的:

Multiline strings can use shell-style "here doc" syntax, with the string starting with a marker like <<EOT and then the string ending with EOT on a line of its own. The lines of the string and the end marker must not be indented.

Terraform 中的 Heredocs 对周围的空白特别有趣。

将您的示例更改为以下内容似乎可以消除 heredoc 特定错误:

provisioner "remote-exec" {
  inline = [<<EOF
echo hi
EOF,
<<EOF
echo \
hi
EOF
  ]
}

这里根本不需要多个 heredoc,因为内联数组是远程主机上应该 运行 的命令列表。将 heredoc 与跨多行的命令一起使用应该适合您:

provisioner "remote-exec" {
  inline = [<<EOF
echo foo
echo bar
EOF
  ]
}