【问题标题】:Escaping quotes in output string in Terraform在 Terraform 的输出字符串中转义引号
【发布时间】:2021-05-07 13:40:03
【问题描述】:

我正在使用 Terraform 0.14.2 开发一个 API 项目。我想创建一个输出来打印我可以在开发 API 时使用的调试 CURL 命令。

这是我尝试输出的示例

curl -X POST -H 'Content-Type: application/json' -d '{"test": "yes"}' https://foo.bar.baz

我的 Terraform 输出如下所示:

output "test" {
  value = "curl -X POST -H 'Content-Type: application/json' -d '${jsonencode({test = "yes"})}' https://foo.bar.baz"
}

Terraform 对此很满意,但它会生成以下实际输出

cur -X POST -H 'Content-Type: application/json' -d '{\"test\": \"yes\"}' https://foo.bar.baz

我尝试手动编写 JSON:

output "test" {
  value = "curl -X POST -H 'Content-Type: application/json' -d '{\"test\" = \"yes\"}' https://foo.bar.baz"
}

但我得到相同的输出。字符串转义在 terraform 中的工作方式是否与其他所有语言不同?举一个更简单的例子,使用这个 terraform:

output "quote" {
  value = "\""
}

这输出"\"" 我希望它输出"

【问题讨论】:

    标签: terraform


    【解决方案1】:

    您想要的是原始输出。您必须明确请求这样的输出:

    terraform output -raw test
    

    但是,我不确定您是否可以强制 terraform apply 使用原始输出,而不是转义输出。

    【讨论】:

    • 确实,terraform apply 输出仅用于演示,因此它使用类似于 Terraform 语言本身的语法。正如 Marcin 所说,terraform output -raw 是获得与其他软件集成的原始价值的预期方式。
    猜你喜欢
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2013-01-06
    相关资源
    最近更新 更多