Terraform v0.12 多行字符串 EOF shell 样式“here doc"v0.11 未像以前那样解释语法 [英] Terraform v0.12 Multi-line String EOF shell-style "here doc" syntax not been interpreted as before with v0.11

查看:32
本文介绍了Terraform v0.12 多行字符串 EOF shell 样式“here doc"v0.11 未像以前那样解释语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Octopus Deploy 中,我使用他们的 Apply a Terraform 模板设置了 Terraform Apply 步骤

Within Octopus Deploy I've setup a Terraform Apply Step using their Apply a Terraform template

在我的 Terraform main.tf 文件中,我想使用连接在 AWS 中的 Amazon Linux EC2 实例上运行远程执行程序

In my Terraform main.tf file I want to use a connection to run an remote-exec on a Amazon Linux EC2 instance in AWS

    resource "aws_instance" "nginx" {
      ami           = "${var.aws_ami}"
      instance_type = "t2.nano"
      key_name      = "${var.key_name}"

      connection {
        type        = "ssh"
        user        = "ec2-user"
        private_key = "${var.aws_key_path}"
      }

      provisioner "remote-exec" {
        inline = [
          "sudo amazon-linux-extras install epel -y",
          "sudo yum update -y",
          "sudo amazon-linux-extras install nginx1.12 -y",
          "sudo systemctl enable nginx.service",
          "sudo systemctl start nginx.service",
          "sudo systemctl status nginx.service"
        ]
      }
    }

作为连接块的一部分,我们需要使用 SSH 密钥对使用私钥 PEM 进行连接,以使用存储在 AWS 上的公钥进行身份验证

As part of the connection block we need to connect using an SSH key pair using a Private Key PEM to auth with the Public Key stored on AWS

我的私钥在 Octopus deploy 中作为变量存储在我的项目中

My Private Key is stored as a variable in my Project in Octopus deploy

为了让我的私钥在 Terraform 中被正确解释为多行字符串,我必须使用 'here doc' 语法,使用起始 EOF 和结束 EOF

For my private key to be interpreted correctly in Terraform as a multi-line string I had to use 'here doc' syntax using a starting EOF and an ending EOF

这个语法解释可以在 Terraform 官方文档中找到

This syntax explanation can be found on the Terraform official documentation at

https://www.terraform.io/docs/configuration-0-11/syntax.html

这是我最初的问题,因为我没有正确处理多行 PEM 文件,所以我的变量语法失败了,我通过 Octopus Deploy Support 提出了以下问题

This was my original problem that my variable syntax was falling over as I wasn't handling the multi-line PEM file correctly and I raised the ticket below with Octopus Deploy Support

https://help.octopus.com/t/terraform-apply-step-pem-variable-set-to-unix-lf-ucs-2-le-bom/23659

他们好心地向我指出了 EOF 语法的方向

Where they kindly were able to point me in the direction of the EOF syntax

这一切在 Terraform v0.11 上运行良好,但我们这里有很多代码是用 v0.12 中的最新 HCL2 编写的

This all worked great on Terraform v0.11 but we've a lot of code here on this side that's been written in the latest HCL2 in v0.12

所以我想强制 Octopus Deploy 使用 v0.12 二进制文件,而不是 Octopus Deploy 附带的预打包 v0.11.他们提供了一个内置的 Special var,所以你可以使用不同的二进制文件

So I wanted to force Octopus Deploy to use a v0.12 binary rather than the prepackaged v0.11 that Octopus Deploy comes with. And they offer a built in Special var so you can use a different binary

但是当我使用这个二进制文件运行它时,脚本会出现以下错误

But when I run it with this binary the script blows up with the error below

Error: Unterminated template string
No closing marker was found for the string. 
August 6th 2019 14:54:07 Error
Calamari.Integration.Processes.CommandLineException: The following command: "C:Program FilesOctopus DeployOctopusin	erraform.exe" apply -no-color -auto-approve -var-file="octopus_vars.tfvars" 
August 6th 2019 14:54:07 Error
With the working directory of: C:OctopusWork20190806135350-47862-353staging 
August 6th 2019 14:54:07 Error
Failed with exit code: 1 
August 6th 2019 14:54:07 Error
Error: Unterminated template string 
August 6th 2019 14:54:07 Error
  on octopus_vars.tfvars line 34:

我查看了 v0.12 的官方文档

I've had a look at the official documentation for v0.12

https://www.terraform.io/docs/configuration/syntax.html#terraform-syntax

而且我不确定在如何管理他们在 v0.11 中拥有的多线方面是否有任何帮助

And I'm not sure if there is anything that helps in relation to how to manage multi-line that they had in v0.11

这是我的 tfvars 文件中在 v0.11 中成功运行的代码块

Here is the code block that worked in v0.11 successfully from my tfvars file

aws_ami = "#{ami}"
key_name = "#{awsPublicKey}"
aws_private_key = <<-EOF
#{testPrivateKey}
-EOF

当我使用最新版本的 Terraform v0.12.6 运行它时,预期的结果是它可以正常运行并在 Octopus Deploy 中运行我的 Terraform 应用

The expected result when I ran this with the latest version of Terraform v0.12.6 was that it would function normally and run my Terraform Apply within Octopus Deploy

我希望 Hashicorp 的某个人对此有解决方法,因为我认为这应该通过 https://github.com/hashicorp/terraform/pull/20281

My hope here is that someone from Hashicorp has a workaround for this as I see this was supposed to be fixed with https://github.com/hashicorp/terraform/pull/20281

但我在写这篇今天下载的 v0.12.6 时使用的是最新的二进制文件

But I'm using the latest binary at the time of writing this v0.12.6 downloaded today

关于如何在 v0.12 中使用此功能的任何建议?干杯

Any suggestions anyone on how to get this working in v0.12? Cheers

推荐答案

flush heredoc"的正确语法不包括最后标记上的破折号:

The correct syntax for a "flush heredoc" does not include a dash on the final marker:

aws_key_path = <<-EOF
#{martinTestPrivateKey}
EOF

如果以前的版本接受 -EOF 来结束heredoc,那么不幸的是这是一个错误,现在已在 Terraform 0.12 中修复,因此向前推进您必须使用 文档中的语法,最后一行只有标记.

If prior versions were accepting -EOF to end the heredoc then that unfortunately was a bug, which has now been fixed in Terraform 0.12 and so moving forward you must use the syntax as documented, with the marker alone on the final line.

这篇关于Terraform v0.12 多行字符串 EOF shell 样式“here doc"v0.11 未像以前那样解释语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆