如何在Terraform中的命令块中处理多行

发布于 2025-02-07 14:12:14 字数 3176 浏览 2 评论 0原文

我有一项任务,我必须在Terraform中创建线束代表,并且必须通过GitHub Action进行模块化和处理。结果,我的主要TF基本上是试图tail文件,这使模块化目录中的kubernetes-harness-delagate文件夹。但是,我基本上必须运行很多命令,一个是我必须创建名为util.sh.sh的脚本,我真的很想让本地提供者处理它,但是Terraform似乎是成为多行的棍子。有没有办法在一个命令中进行所有操作,而无需执行多行和/或使用Heredoc语法?

要明确我不想在repo中做任何东西,我希望以下块在我的命令 e节中创建它

。对于其他命令,我正在尝试弄清楚如何使其在一个命令中运行多个命令,而不会分解所有命令。

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    #command     = "pwd && ls -la && return 1"
    command     = "tar -xvf harness-delegate-kubernetes.tar"
  }
}

我尝试使用以下\ n作为Terraform Doc在每行末尾建议的,但仍然不喜欢它,但我仍然会收到一个错误:

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    #command     = "pwd && ls -la && return 1"
    command     = "tar -xvf harness-delegate-kubernetes.tar \n
                  && cat <<-EOF > util.sh
                  SRC_FILE=harness-delegate.yaml
                  TMP_FILE=harness-delegate.tmp
                  DELEGATE_NAME=$1
                  if [ $# -lt 1 ]; then
                    echo "usage utils.sh <name>"
                    exit 0
                  fi
                  DST_FILE=harness-${DELEGATE_NAME}.yaml
                  if [ -f $DST_FILE ]; then
                    echo "File $DST_FILE exists. Exiting..."
                    exit 1
                  fi
                  if [ ! -f ${TMP_FILE} ]; then
                  echo "creating $TMP_FILE"
                  cp $SRC_FILE $TMP_FILE
                  dname=$(sed -n "1,/^.*harness.io.name/s?^.*harness.io/name: ??p" $TMP_FILE)
                  sed -i -e "s/$dname/DELEGATENAME/" $TMP_FILE
                  fi

                  echo "creating $DST_FILE"
                  cp $TMP_FILE $DST_FILE
                  sed -i -e "s/DELEGATENAME/${DELEGATE_NAME}/" $DST_FILE
                  EOF"
  }
}

有关多个行的错误消息

Invalid multi-line string: Quoted strings may not be split over multiple lines. To produce a multi-line string, either use the \n escape to represent a newline character or use the "heredoc" multi-line template syntax.HCL

我如何处理1个命令块中的多个命令,以便&lt; all_commands_that_need_to_to_be_run&gt;在主引号命令条目中

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    #command     = "pwd && ls -la && return 1"
    command     = "<all_commands_that_need_to_be_run>"
  }
}

I have a task whereby which I have to create a Harness Delegate in Terraform, and it has to be modularized and handled by GitHub Actions. As a result, my main TF is basically trying to tar a file, which makes a kubernetes-harness-delagate folder in the modularized directory. However, there are a bunch of commands I basically have to run and one is I have to create the script called util.sh and I would really like for the local provisioner to handle it, but Terraform seems to be a stickler for multiple lines. Is there a way to do this all in one command without having to do multiple lines and/or use heredoc syntax?

To be clear I don't want to make any of this in the repo, I want the following block to create it within the command section in my main.tf:

The following works within my repo and will tar the folder and create it, but I need it to other commands as well and I am trying to figure out how to make it run multiple commands within the one command without splitting out all of the commands.

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    #command     = "pwd && ls -la && return 1"
    command     = "tar -xvf harness-delegate-kubernetes.tar"
  }
}

I tried the following using \n as terraform docs suggest at the end of each line but it still didn't like it and I still get an error:

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    #command     = "pwd && ls -la && return 1"
    command     = "tar -xvf harness-delegate-kubernetes.tar \n
                  && cat <<-EOF > util.sh
                  SRC_FILE=harness-delegate.yaml
                  TMP_FILE=harness-delegate.tmp
                  DELEGATE_NAME=$1
                  if [ $# -lt 1 ]; then
                    echo "usage utils.sh <name>"
                    exit 0
                  fi
                  DST_FILE=harness-${DELEGATE_NAME}.yaml
                  if [ -f $DST_FILE ]; then
                    echo "File $DST_FILE exists. Exiting..."
                    exit 1
                  fi
                  if [ ! -f ${TMP_FILE} ]; then
                  echo "creating $TMP_FILE"
                  cp $SRC_FILE $TMP_FILE
                  dname=$(sed -n "1,/^.*harness.io.name/s?^.*harness.io/name: ??p" $TMP_FILE)
                  sed -i -e "s/$dname/DELEGATENAME/" $TMP_FILE
                  fi

                  echo "creating $DST_FILE"
                  cp $TMP_FILE $DST_FILE
                  sed -i -e "s/DELEGATENAME/${DELEGATE_NAME}/" $DST_FILE
                  EOF"
  }
}

Error Message regarding multiple lines:

Invalid multi-line string: Quoted strings may not be split over multiple lines. To produce a multi-line string, either use the \n escape to represent a newline character or use the "heredoc" multi-line template syntax.HCL

How can I handle multiple commands within 1 command block such that <all_commands_that_need_to_be_run> are within the main quoted command entry:

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    #command     = "pwd && ls -la && return 1"
    command     = "<all_commands_that_need_to_be_run>"
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是暖光i 2025-02-14 14:12:14

看起来有一种方法可以使用heredoc来完成此操作,使您可以使用local-exec provisioner在一个命令中运行多个命令。尽管Terraform文档在告诉您如何使用它方面很糟糕。

示例:

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command = <<EOT
      exec tar -xvf harness-delegate-kubernetes.tar
      exec cp ../../util.sh harness-delegate-kubernetes/
      exec chmod a+x harness-delegate-kubernetes/util.sh
      exec .harness-delegate-kubernetes/util.sh 
    EOT
  }
}

命令:

    command = <<EOT
      exec tar -xvf harness-delegate-kubernetes.tar
      exec cp ../../util.sh harness-delegate-kubernetes/
      exec chmod a+x harness-delegate-kubernetes/util.sh
      exec .harness-delegate-kubernetes/util.sh 
    EOT

Looks like there is a way to do it with heredoc that allows you to run multiple commands in one command using the local-exec provisioner. Though the Terraform docs are terrible in telling you how to use it.

Example:

resource "null_resource" "delegate" {
  triggers = {
    cluster_arn = module.primary.cluster_arn
  }
  provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    command = <<EOT
      exec tar -xvf harness-delegate-kubernetes.tar
      exec cp ../../util.sh harness-delegate-kubernetes/
      exec chmod a+x harness-delegate-kubernetes/util.sh
      exec .harness-delegate-kubernetes/util.sh 
    EOT
  }
}

Commands:

    command = <<EOT
      exec tar -xvf harness-delegate-kubernetes.tar
      exec cp ../../util.sh harness-delegate-kubernetes/
      exec chmod a+x harness-delegate-kubernetes/util.sh
      exec .harness-delegate-kubernetes/util.sh 
    EOT
抚笙 2025-02-14 14:12:14

我建议您在bash脚本中运行这些命令,并从您的local-exec provisioner中调用它,例如:

command = "chmod +x ${path.module}/myscript.sh && ${path.module}/my_script.sh

I would recommend you to run those commands in a bash script and call it from your local-exec provisioner, for readability purposes, e.g.:

command = "chmod +x ${path.module}/myscript.sh && ${path.module}/my_script.sh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文