英文:
Terraform local provisioner adds '$'\r to filenames
问题
这是我的代码:
resource "tls_private_key" "create_key" {
algorithm = "RSA"
rsa_bits = 4096
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = <<EOT
mkdir keypairs
cd keypairs
echo "${tls_private_key.create_key.private_key_pem}" > "${var.deployment_name}.pem"
echo "${tls_private_key.create_key.public_key_openssh}" > "${var.deployment_name}.pem.pub"
EOT
}
}
然而,似乎会在目录名和文件名中添加'$'\r
。例如,生成的目录将被命名为:keypairs'$'\r
。
我使用VS Code上的Terraform,在Ubuntu 20.04 WSL上运行。有什么办法可以解决这个问题吗?我阅读了不同的来源,尝试了各种方法,但没有找到解决方案。
英文:
I am using terraform to create pem files.
This is my code:
resource "tls_private_key" "create_key" {
algorithm = "RSA"
rsa_bits = 4096
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = <<-EOT
mkdir keypairs
cd keypairs
echo "${tls_private_key.create_key.private_key_pem}" > "${var.deployment_name}.pem"
echo "${tls_private_key.create_key.public_key_openssh}" > "${var.deployment_name}.pem.pub"
EOT
}
}
However, this seems to add '$'\r
at both the directory name and the file names.
For example, resulting directory would be named: keypairs'$'\r
I am using Terraform with VS Code, running on Ubuntu 20.04 WSL.
Any idea how I can get around this?
Read different sources, tried various things but not found a solution.
答案1
得分: 0
在这个配置的`<<-EOT` ... `EOT`部分,Terraform会将这些行的行尾原样保留在您的文件中。
看起来您将这个文件保存为Windows风格的行尾(CRLF),因此该模板的有效结果也具有Windows风格的行尾。`bash`似乎无法支持Windows风格的行尾,因此CR部分被解释为命令行的一部分,而不是行分隔符的一部分。
如果您将此`.tf`文件以Unix风格的行尾保存(只使用LF),那么这些行尾将包含在渲染模板的结果中,我预计这将实现您想要的结果。
---
[Provisioners are a last resort](https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax#provisioners-are-a-last-resort),所以这里有一种使用资源获得该结果的不同方法:
```plaintext
terraform {
required_providers {
tls = {
source = "hashicorp/tls"
}
local = {
source = "hashicorp/local"
}
}
}
resource "tls_private_key" "create_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "local_file" "private_key" {
filename = "${var.deployment_name}.pem"
sensitive_content = tls_private_key.create_key.private_key_pem
}
resource "local_file" "public_key" {
filename = "${var.deployment_name}.pem.pub"
content = tls_private_key.create_key.public_key_openssh
}
英文:
Inside the <<-EOT
... EOT
section of this configuration Terraform will take the line endings of those lines exactly as they are in your file.
It seems like you've saved this file with Windows-style line endings (CRLF) and so the effective result of that template also has Windows-style line endings. bash
seems to be unable to support Windows-style line endings and so the CR part is being interpreted as part of the command line, rather than as part of the line separator.
If you save this .tf
file with Unix-style line endings instead (just LF) then those line endings will be included in the result of rendering the template, which I expect will achieve the result you wanted.
Provisioners are a last resort, so here's a different way to get that result using resources:
terraform {
required_providers {
tls = {
source = "hashicorp/tls"
}
local = {
source = "hashicorp/local"
}
}
}
resource "tls_private_key" "create_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "local_file" "private_key" {
filename = "${var.deployment_name}.pem"
sensitive_content = tls_private_key.create_key.private_key_pem
}
resource "local_file" "public_key" {
filename = "${var.deployment_name}.pem.pub"
content = tls_private_key.create_key.public_key_openssh
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论