You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Terraform中输出templatefile生成的SSH配置且无heredoc标记?

如何移除Terraform输出SSH配置时的Heredoc标记

我用Terraform构建基础设施时,希望输出新创建服务器的格式化SSH配置,于是编写了如下模板文件:

Host ${serverLabel}
    HostName ${hostname}
    User ${user}
    IdentityFile ${identityFile}

并在output块中引用该模板:

output "ssh-config" {
  description = "The new server's ssh config"
  value = templatefile("${path.module}/ssh-config.tpl", {
    serverLabel=aws_instance.minecraft_ec2.tags.Name
    hostname= aws_instance.minecraft_ec2.public_ip
    user= "ec2-user",
    identityFile = "~/.ssh/minecraft_key"
  })
}

功能正常,但输出时会显示Heredoc标记,虽然可以复制标记内的内容,但希望能直接输出干净的配置内容。

解决方法

使用terraform output命令时添加--raw参数,即可避免显示Heredoc标记:

terraform output --raw ssh-config

该参数会直接输出原始字符串,不会添加Heredoc这类特殊格式标记,符合直接获取可用SSH配置的需求。

内容的提问来源于stack exchange,提问作者Chris Schmitz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 06:05:31