Terraform - 无法在本地 exec 中运行多个命令 [英] Terraform - Unable to run multiple commands in local exec

查看:25
本文介绍了Terraform - 无法在本地 exec 中运行多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Terraform 世界的新手.我正在尝试使用 Terraform 运行 shell 脚本.

I'm new to Terraform world. I'm trying to run a shell script using Terraform.

下面是main.tf文件

Below is the main.tf file

#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 文件

Below is the install-istio.sh file which it needs to run

#!/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.

main.tf 中的上述脚本确实在后台运行命令.

The above script in main.tf does run command in the background.

有人可以帮我解决缺失的部分吗?如何使用本地 exec 运行多个命令另外,如何消除警告消息?

Can someone help me with the missing part? How can I run multiple commands using local exec Also, How can I get rid of the warning message?

感谢您的帮助,谢谢!

推荐答案

我认为这里有两个独立的事情实际上并不相关.

I think there are two separate things going on here which are actually not related.

这里的主要问题在于您如何编写 local-exec 脚本:

The main problem here is in how you've written your local-exec script:

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

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

This will become the following shell script to run:

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

通过将第一个命令行放在引号中,您告诉 shell 尝试运行一个名为 chmod +x install-istio.sh 的程序,没有任何参数.也就是说,shell 将尝试在您的 PATH 中找到一个名为 chmod +x install-istio.sh 的可执行文件,而不是尝试运行一个名为 的命令chmod 带有一些我认为你想要的参数.

By putting the first command line in quotes, you're telling the shell to try to run a program that is called chmod +x install-istio.sh without any arguments. That is, the shell will try to find an executable in your PATH called chmod +x install-istio.sh, rather than trying to run a command called just chmod with some arguments as I think you intended.

删除命令行周围的引号以使其工作.这里不需要引号,因为这些命令都不包含任何需要引用的特殊字符:

Remove the quotes around the command lines to make this work. Quotes aren't needed here because neither of these commands contain any special characters that would require quoting:

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


有关仅插值表达式的警告消息与运行这些命令的问题无关.这表明您使用了仍然支持向后兼容但不再推荐的旧语法.


The warning message about interpolation-only expressions is unrelated to the problem of running these commands. This is telling you that you've used a legacy syntax that is still supported for backward compatibility but no longer recommended.

如果您在撰写本文时使用的是最新版本的 Terraform(v0.15 版本之一或更高版本),那么您可以通过切换到此模块目录并运行来解决此问题和其他类似警告terraform fmt,这是一个命令更新您的配置以匹配预期的样式约定.

If you are using the latest version of Terraform at the time of writing (one of the v0.15 releases, or later) then you may be able to resolve this and other warnings like it by switching into this module directory and running terraform fmt, which is a command that updates your configuration to match the expected style conventions.

或者,您可以手动更改该命令将自动更新的内容,即删除 path.module 周围的冗余插值标记:

Alternatively, you could manually change what that command would update automatically, which is to remove the redundant interpolation markers around path.module:

    working_dir = path.module

这篇关于Terraform - 无法在本地 exec 中运行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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