0

I would like to create a templating system that executes bash within a text file.

For example, let's consider we created a simple template.yaml file:

my_path: $(echo ${PATH})
my_ip: $(curl -s http://whatismyip.akamai.com/)
some_const: "foo bar"
some_val: $(echo -n $MY_VAR | base64)

The desire is to execute each one, such that the result may look like:

my_path: /Users/roman/foo
my_ip: 1.2.3.4
some_const: "foo bar"
some_val: ABC

How would I go about doing such a substitution?


Reasons for wanting this:

  • There are many values, and doing something like a sed or envsubst isn't practical
  • It would be common to apply a series of piped transformations
  • The configuration file would be populated from numerous sources, all of them essentially bash commands
  • I do need to create a yaml file of a specific format (ultimately used by another tool)
  • I could create aliases etc to increase readability
  • By having it execute in it's own shell none of the semi-sensitive values are stored as in history or as files.

I'm not married to this approach, and would happily attempt a recommendation that fulfils the reasons.

Roman
  • 8,826
  • 10
  • 63
  • 103

2 Answers2

0

This might work, you can try though: you can create a script: script.sh which will take one argument as .yaml file and will expand the variables inside that file:

script.sh :

    echo 'cat <<EOF' >  temp.sh
    cat "$1"         >> temp.sh
    echo 'EOF'       >> temp.sh
    bash temp.sh
    rm temp.sh

and you can invoke the script as from the command line : ./script.sh template.yaml

User123
  • 1,498
  • 2
  • 12
  • 26
0

Thank you to @joshmeranda for pointing me in the right direction, this solved my problem

echo -e "$(eval "echo -e \"`<template.yaml`\"")"

While eval can be dangerous, in my case its usage is controlled.

Roman
  • 8,826
  • 10
  • 63
  • 103