1

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?

Codious-JR
  • 1,658
  • 3
  • 26
  • 48

1 Answers1

0

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")}"
    }
RyanKim
  • 1,557
  • 9
  • 10
  • thanks for the response. The External Data Source looks like a perfect solution to my problem. I have a question, because I don't understand completely the execution logic in terraform. Does the program in the external data source get executed when the result is referenced, or it is executed earlier? It could give an error if the program is executed before configuring the gitlab runner and so the config file will not be present. – Codious-JR Jul 15 '19 at 15:05
  • 1
    https://learn.hashicorp.com/terraform/getting-started/dependencies.html#implicit-and-explicit-dependencies Terraform makes dependency order automatically and you don't worry about it. or you can use depends_on, explicit dependency between resources. – RyanKim Jul 15 '19 at 15:34