10

I am trying to use a multiline string in the provisioner "remote-exec" block of my terraform script. Yet whenever I use the EOT syntax as outlined in the documentation and various examples I get an error that complains about having: invalid characters in heredoc anchor.

Here is an example of a simple provisioner "remote-exec" that received this error (both types of EOT receive this error when attempted separately):

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

    << EOT 
    echo \
    hi 
    EOT,
  ]
}

Update: Here is the working solution, read carefully if you are having this issue because terraform is very picky when it comes to EOF:

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

   echo foo
   echo bar

  EOF
  ]
}

Note that if you want to use EOF all the commands you use in a provisioner "remote-exec" block must be inside the EOF. You cannot have both EOF and non EOF its one or the other.

The first line of EOF must begin like this, and you cannot have any whitespace in this line after <<EOF or else it will complain about having invalid characters in heredoc anchor:

  inline = [<<EOF

Your EOF must then end like this with the EOF at the same indentation as the ]

  EOF
  ]
4

2 回答 2

10

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

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

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

尽管内联数组是应在远程主机上运行的命令列表,但您根本不需要在此处使用多个 heredocs。将heredoc与多行命令一起使用对您来说应该可以正常工作:

provisioner "remote-exec" {
  inline = [<<EOF
echo foo
echo bar
EOF
  ]
}
于 2016-06-19T19:45:25.117 回答
1

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

试试这个:

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

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

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

事实上,Terraform 文档是这样说的:

多行字符串可以使用 shell 风格的“here doc”语法,字符串以标记 like 开头,<<EOT然后字符串以EOT自己的一行结尾。字符串的行和结束标记不能缩进。

于 2016-06-17T17:12:40.323 回答