linux Terraform -无法在本地执行中运行多个命令

zzoitvuj  于 2023-05-06  发布在  Linux
关注(0)|答案(2)|浏览(225)

我是新来的。我正在尝试使用Terraform运行一个shell脚本。
下面是main.tf文件

#Executing shell script via Null Resource

resource "null_resource" "install_istio" {
 provisioner "local-exec" {
    command = <<EOT
      "chmod +x install-istio.sh"
      "./install-istio.sh"
    EOT
    interpreter = ["/bin/bash", "-c"]
    working_dir = "${path.module}"
  }
}

下面是install-istio.sh它需要运行的www.example.com文件

#!/bin/sh

# Download and install the Istio istioctl client binary

# Specify the Istio version that will be leveraged throughout these instructions
ISTIO_VERSION=1.7.3

curl -sL "https://github.com/istio/istio/releases/download/$ISTIO_VERSION/istioctl-$ISTIO_VERSION-linux-amd64.tar.gz" | tar xz

sudo mv ./istioctl /usr/local/bin/istioctl
sudo chmod +x /usr/local/bin/istioctl

# Install the Istio Operator on EKS
istioctl operator init

# The Istio Operator is installed into the istio-operator namespace. Query the namespace.
kubectl get all -n istio-operator

# Install Istio components
istioctl profile dump default

# Create the istio-system namespace and deploy the Istio Operator Spec to that namespace.
kubectl create ns istio-system
kubectl apply -f istio-eks.yaml

# Validate the Istio installation
kubectl get all -n istio-system

我得到了以下警告:

Warning: Interpolation-only expressions are deprecated
  on .terraform/modules/istio_module/Istio-Operator/main.tf line 10, in resource "null_resource" "install_istio":
  10:     working_dir = "${path.module}"
Terraform 0.11 and earlier required all non-constant expressions to be
provided via interpolation syntax, but this pattern is now deprecated. To
silence this warning, remove the "${ sequence from the start and the }"
sequence from the end of this expression, leaving just the inner expression.
Template interpolation syntax is still used to construct strings from
expressions when the template includes multiple interpolation sequences or a
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

www.example.com中的上述脚本main.tf在后台运行命令。
有人能帮我补上缺失的部分吗?如何使用本地exec运行多个命令以及如何摆脱警告消息?
感谢您的帮助,谢谢!

iswrvxsc

iswrvxsc1#

我认为这里发生了两件独立的事情,实际上并不相关。
这里的主要问题是如何编写local-exec脚本:

command = <<EOT
      "chmod +x install-istio.sh"
      "./install-istio.sh"
    EOT

这将成为要运行的以下shell脚本:

"chmod +x install-istio.sh"
"./install-istio.sh"

通过将第一个命令行放在引号中,您告诉shell尝试运行一个名为chmod +x install-istio.sh的程序,而不带任何参数。也就是说,shell将尝试在PATH中找到名为chmod +x install-istio.sh的可执行文件,而不是像我想的那样,尝试运行名为chmod的命令和一些参数。
删除命令行周围的引号以使其正常工作。这里不需要引号,因为这两个命令都不包含任何需要引号的特殊字符:

command = <<-EOT
      chmod +x install-istio.sh
      ./install-istio.sh
    EOT

有关仅插值表达式的警告消息与运行这些命令的问题无关。这是在告诉您,您使用了一种遗留语法,该语法仍受向后兼容性支持,但不再推荐使用。
如果您在撰写本文时使用的是最新版本的Terraform(v0.15或更高版本之一),那么您可以通过切换到此模块目录并运行terraform fmt来解决此问题和其他类似的警告,这是一个更新配置以匹配预期样式约定的命令。
或者,您可以手动更改该命令将自动更新的内容,即删除path.module周围的冗余插值标记:

working_dir = path.module
fcipmucu

fcipmucu2#

请注意,对于运行多个命令Terraform建议使用多个provisioners(参考)。多个置备程序将按照配置文件中定义的顺序执行。例如:

resource "null_resource" "install_istio" {
  # ...

  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    working_dir = "${path.module}"
    command = "chmod +x install-istio.sh"
  }

  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    working_dir = "${path.module}"
    command = "./install-istio.sh"
  }
}

我理解在某些情况下,这可能是一种开销,因为相同的东西(解释器和working_dir)应该被填充多次,但在其他情况下,它提供了灵活性,可以完全按照您想要的方式运行。

相关问题