Terraform 转义序列

Pra*_*hel 4 terraform

我正在我的 terraform 文件中使用远程执行配置程序在我的 ec2 上运行一些命令。但我坚持在命令中转义特殊字符。此代码部分来自远程执行配置程序部分内的 main.tf 文件。terraform 中出现的错误是“无效字符”和“无效多行字符串”。我想要正确的字符串序列,以便这些命令可以在我的 ec2 上执行。

"VAR=$(cat contents.txt | grep '"token"'),
"VAR="${VAR:11}"",
"VAR="${VAR:0:-1}"",
Run Code Online (Sandbox Code Playgroud)

use*_*247 9

它们${也被 terraform 解释(作为变量替换)。你需要逃避那些要$成为的人$${

一个完整的工作示例:

主要.tf:

resource "null_resource" "test" {
  provisioner "local-exec" {
    command = <<EOF
echo 'some11char hello "token"' > contents.txt

VAR=$(cat contents.txt | grep \"token\")
VAR=$${VAR:11}
VAR=$${VAR:0:5}

echo $VAR >log
    EOF
  }
}
Run Code Online (Sandbox Code Playgroud)
$ terraform apply -input=false -auto-approve 
null_resource.test: Creating...
null_resource.test: Provisioning with 'local-exec'...
null_resource.test (local-exec): Executing: ["/bin/sh" "-c" "echo 'some11char hello \"token\"' > contents.txt\n\nVAR=$(cat contents.txt | grep \\\"token\\\")\nVAR=${VAR:11}\nVAR=${VAR:0:5}\n\necho $VAR >log\n"]
null_resource.test: Creation complete after 0s [id=3425651808766026549]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Run Code Online (Sandbox Code Playgroud)
$ cat contents.txt 
some11char hello "token"

$ cat log     
hello

$
Run Code Online (Sandbox Code Playgroud)