首页 > 解决方案 > 如何使用 template_file 从另一个 shell 执行 shell 脚本?

问题描述

我正在使用 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

标签: terraform

解决方案


我会创建 2 个 data source template_file,一个用于main ,other_script.sh一个用于 main 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
}

推荐阅读