Terraform本地供应商在文件名后添加了’$’, ‘$’符号。

huangapple go评论104阅读模式
英文:

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 &quot;tls_private_key&quot; &quot;create_key&quot; {
   algorithm     = &quot;RSA&quot;
   rsa_bits      = 4096

   provisioner &quot;local-exec&quot; {
     interpreter = [&quot;/bin/bash&quot;, &quot;-c&quot;]
     command   = &lt;&lt;-EOT
        mkdir keypairs
        cd keypairs
        echo &quot;${tls_private_key.create_key.private_key_pem}&quot;     &gt; &quot;${var.deployment_name}.pem&quot;
        echo &quot;${tls_private_key.create_key.public_key_openssh}&quot;  &gt; &quot;${var.deployment_name}.pem.pub&quot;
    EOT
  }
}

However, this seems to add &#39;$&#39;\r at both the directory name and the file names.
For example, resulting directory would be named: keypairs&#39;$&#39;\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

在这个配置的`&lt;&lt;-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 = &quot;hashicorp/tls&quot;
    }
    local = {
      source = &quot;hashicorp/local&quot;
    }
  }
}

resource &quot;tls_private_key&quot; &quot;create_key&quot; {
  algorithm     = &quot;RSA&quot;
  rsa_bits      = 4096
}

resource &quot;local_file&quot; &quot;private_key&quot; {
  filename          = &quot;${var.deployment_name}.pem&quot;
  sensitive_content = tls_private_key.create_key.private_key_pem
}

resource &quot;local_file&quot; &quot;public_key&quot; {
  filename = &quot;${var.deployment_name}.pem.pub&quot;
  content  = tls_private_key.create_key.public_key_openssh
}
英文:

Inside the &lt;&lt;-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 = &quot;hashicorp/tls&quot;
    }
    local = {
      source = &quot;hashicorp/local&quot;
    }
  }
}

resource &quot;tls_private_key&quot; &quot;create_key&quot; {
  algorithm     = &quot;RSA&quot;
  rsa_bits      = 4096
}

resource &quot;local_file&quot; &quot;private_key&quot; {
  filename          = &quot;${var.deployment_name}.pem&quot;
  sensitive_content = tls_private_key.create_key.private_key_pem
}

resource &quot;local_file&quot; &quot;public_key&quot; {
  filename = &quot;${var.deployment_name}.pem.pub&quot;
  content  = tls_private_key.create_key.public_key_openssh
}

huangapple
  • 本文由 发表于 2023年4月6日 22:12:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定