【问题标题】:How do you execute a shell script from another shell using template_file?如何使用 template_file 从另一个 shell 执行 shell 脚本?
【发布时间】:2019-03-12 22:03:33
【问题描述】:

我正在使用 TEMPLATE_FILE 指令为新的 GCP Compute Engine 实例执行引导 bash 脚本(称为“startup-script.sh”)。我创建了一个包含我的脚本的数据模板文件条目(见下文)。但是,从我的“startup-script.sh”脚本中,我想执行另一个包含函数和环境变量的脚本。换句话说,类似“.../some-directory/some-script.sh”这样的函数和变量可用于我的 startup-script.sh 脚本。我不清楚如何做到这一点。如果我尝试从我的 startup-script.sh 脚本中执行“some-script.sh”,则找不到它。任何建议表示赞赏。

data "template_file" "startup-script" {
  template = "${file("startup-script.sh")}"

  vars {.... 

“startup-script.sh”只是一个 bash 脚本,发出“terraform apply”时失败的调用看起来很简单:

#!/bin/bash

. ../other_dir/other_script.sh

【问题讨论】:

  • startup-script.sh 和您的其他脚本是什么样的?你试过采购吗?我不明白为什么那行不通,但也许我误解了您要做什么。
  • 这只是一个 bash shell 脚本。如果您在另一个目录中引用源 shell 脚本,那么当您应用模板时,它找不到该脚本。申请将失败。

标签: terraform


【解决方案1】:

我会创建两个数据源template_files,一个用于other_script.sh,一个用于主startup-script.sh,然后将它们组合起来。

data "template_file" "startup-script" {
  template = "${file("startup-script.sh")}"
}

data "template_file" "other_script" {
  template = "${file("other_script.sh")}"
}

resource "aws_instance" "app" {
  user_data = <<EOF
${data.template_file.other_script.rendered}
${data.template_file.startup-script.rendered}
EOF
}

【讨论】:

    猜你喜欢
    • 2012-01-11
    • 1970-01-01
    • 2013-12-26
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 2019-02-10
    相关资源
    最近更新 更多