Search code examples
terraformterraform-template-file

How to use Bash Commands in Terraform Template File Variables?


I am using Terraform to configure an Auto Scaling Gitlab Runner. I am having issues when overwriting the runner configuration file (for which I am using Terraform Templates), because the file requires the Runner's Unique Token which is generated after registration.

The configuration file looks like this:

concurrent = 1
check_interval = 60

[[runners]]
    name = "POC Group Runner"
    url = "https://gitlab.com/"
    token = "ABCD"
    executor = "docker+machine"
    limit = 1 # max number of docker machines to be created

I want to read this runner token (which is "ABCD") from this file, so that I can use it in a Terraform Template to overwrite the configurations.

In terraform the only "read" function I have is "file", which reads the whole content, and then getting only the token from it becomes a ugly process:

trimspace(replace(split("executor", split("token =", file("/etc/gitlab-runner/config.toml"))[1])[0], "\"", ""))

Is it possible to use Bash Scripts to create variables for templates?

For example I could use the following command to read the token:

cat /etc/gitlab-runner/config.toml | grep "token =" | awk '//{print $3}' | sed 's/"//g'

But how do I feed that into a template?

Can I do something like this? :

Data "template_file" "runner-config" {
    template = "${file("runner-config.toml")"
    vars = {
        runner_token = "`cat /etc/gitlab-runner/config.toml | grep "token =" | awk '//{print $3}' | sed 's/"//g'`"
    }

Does anyone have either a better way of reading something specific from a file, or know how to use bash scripts in templates?


Solution

  • You have similar problem: Terraform external data in metadata_startup_script.

    how about using external data resource? https://www.terraform.io/docs/providers/external/data_source.html

    get_token.sh

    #!/bin/bash
    token=$(cat /etc/gitlab-runner/config.toml | grep "token =" | awk '//{print $3}' | sed 's/"//g')
    jq -n --arg token $token '{token:$token}'
    

    or you can use echo ,instead of jq,

    ...
    echo -n "{\"token\":\"${token}\"}"
    

    and

    data "external" "get_token" {
      program = ["/bin/sh", "${path.module}/get-token.sh"]
    }
    
    data "template_file" "runner-config" {
        template = "${file("runner-config.toml")"
        vars = {
            runner_token = "${lookup(data.external.get_token.result, "token")}"
        }