简体   繁体   English

Terraform不支持shell括号中的变量?

[英]Terraform doesn't support shell variables in brackets?

I am using Terraform v0.13.1.我正在使用 Terraform v0.13.1。 I am trying to use provisioner local-exec to run some shell commands on an EC2 instance:我正在尝试使用 provisioner local-exec 在 EC2 实例上运行一些 shell 命令:

provisioner "local-exec" {
  command = <<EOF
    AWS_PROFILE=test ssh centos@<EC2 instance ID> "filename=test && touch \$filename && mv \$filename \$\{filename//t/a\}"
    EOF
}

I got the following error message after running terraform apply:运行 terraform apply 后出现以下错误信息:

null_resource.test (local-exec): Executing: ["/bin/sh" "-c" "      AWS_PROFILE=test ssh centos@<instance ID> \"filename=test && touch \\$filename && mv \\$filename \\$\\{filename//t/a\\}\"\n"]
null_resource.test (local-exec): mv: cannot move 'test' to '${filename//t/a}' : No such file or directory


Error: Error running command '      AWS_PROFILE=test ssh centos@<instance ID> "filename=test && touch \$filename && mv \$filename \$\{filename//t/a\}"
': exit status 1. Output: mv: cannot move 'test' to '${filename//t/a}' : No such file or directory

It looks like Terraform was able to parse $filename variable after adding escape to the dollar sign.看起来 Terraform 在向美元符号添加转义后能够解析 $filename 变量。 However, shell variables in brackets (${filename}) are not parsed properly in my case even though I added escape to the dollar sign.但是,在我的例子中,括号中的 shell 个变量 (${filename}) 没有被正确解析,即使我在美元符号中添加了转义符。

I also tried $$ and %${ but neither worked.我也尝试了 $$ 和 %${ 但都没有用。 I am lost on this.我对此迷路了。 Can someone please advise whether this is supported in Terraform and what I did wrong?有人可以告诉我 Terraform 是否支持这个吗?我做错了什么?

The below code worked for me:以下代码对我有用:

provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command = <<EOF
        AWS_PROFILE=test ssh centos@<instance ID> "filename=test && touch \$filename && mv \$filename \$${filename//t/a}"
      EOF
    }

Basically need to double up the initial marker and escape dollar signs.基本上需要加倍初始标记并转义美元符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM