Skip to content

Instantly share code, notes, and snippets.

@fmoessbauer
Last active February 13, 2018 08:17
Show Gist options
  • Select an option

  • Save fmoessbauer/3695a4c32a825f9f9ea9dc6a754d5e24 to your computer and use it in GitHub Desktop.

Select an option

Save fmoessbauer/3695a4c32a825f9f9ea9dc6a754d5e24 to your computer and use it in GitHub Desktop.
BASH Script to substitute secrets in configuration files
#!/bin/bash
# Trivial solution to replace placeholders in config files
# Finds all .tpl files, replaces keys and stores the result
# without .tpl
#
# Usage: replacer.sh <keyfile>
#
# Keyfile format:
# key=value
# key2=value2
function replace_in_file {
FILENAME=${1//.tpl/}
[[ $FILENAME == $1 ]] && exit 1
echo "Process $FILENAME"
cp $1 $FILENAME
while read p; do
KEYVAL=(${p//=/ })
sed -i -e "s/<<${KEYVAL[0]}>>/${KEYVAL[1]}/g" $FILENAME
done < $2
}
[ -e "$1" ] || exit 1
echo "Secrets file: $1"
find . -iname "*.tpl" | while read p; do
replace_in_file $p $1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment