How to simplify large task script with functions in template data/source

Currently, we have multiple jobs with quite large task scripts in bash (up to 500 lines) written directly into task/template/data as heredoc. These scripts can have multiple bash functions embedded and use many interpolated variables passed in from the nomad job. I’ve read the doc and know that we could use either artifacts or source instead of data to ship these scripts. However, using artifacts (pulling from s3 or some other places) means we can’t have the script in the same git repo as our nomad job, which can be a pain to synchronize with every change in interpolated variables. As far as I can see, using source it’s possible to keep everything in one repo. I could use a consul template to ship it, which comes back to either heredoc or artifacts.
I found this related issue:

from that thread, it looks like I could use file function to load an external file in the same repo. This looks to be my best bet. I’d like to know what you guys think about this.

For anyone who happens to be searching for a solution to this problem, I came up with one that uses multiple templatefile function calls. For my nomad job, I have:

resource "nomad_job" "myjob" {
  jobspec = templatefile("${path.module}/nomad.tpl", {
    instance_count         = var.instance_count
    my_long_script = templatefile("${path.module}/", {
      var_1 = var.var_1
      var_2 = var.var_2
1 Like