【问题标题】:Terraform escape sequenceTerraform 转义序列
【发布时间】:2021-09-03 08:28:43
【问题描述】:

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

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

【问题讨论】:

  • 目前还不清楚您想要运行的确切命令。你能只写你想执行的命令,而不是转义吗?
  • 我已经完成了...你能验证它是否可以理解
  • 这不是一个有效的 shell 脚本。此外,cat file |grep thing 写成 grep thing file 时更简单。最后,我并没有真正得到您想要做的事情:您将与 content.txt 中的令牌匹配的行存储在 VAR 中。然后,如果它是空的,你将它设置为 11 ?我不明白最后一步。
  • 不,这些是我的 terraform 脚本上的命令。我只需要找到正确的字符串序列,即特殊字符的正确转义字符。

标签: terraform


【解决方案1】:

${ 也被 terraform 解释(作为变量替换)。你需要用$ 转义为$${

一个完整的工作示例:

main.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
  }
}
$ 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.
$ cat contents.txt 
some11char hello "token"

$ cat log     
hello

$

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 2023-03-15
    • 2019-07-28
    • 2021-01-30
    • 2013-11-30
    • 2021-07-01
    • 2012-08-13
    • 1970-01-01
    相关资源
    最近更新 更多