如何在Terraform中的命令块中处理多行
我有一项任务,我必须在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来有一种方法可以使用
heredoc
来完成此操作,使您可以使用local-exec
provisioner在一个命令中运行多个命令。尽管Terraform
文档在告诉您如何使用它方面很糟糕。示例:
命令:
Looks like there is a way to do it with
heredoc
that allows you to run multiple commands in one command using thelocal-exec
provisioner. Though theTerraform
docs are terrible in telling you how to use it.Example:
Commands:
我建议您在bash脚本中运行这些命令,并从您的
local-exec
provisioner中调用它,例如: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.: