Last active
July 13, 2021 11:48
-
-
Save atward/7a2eb1b4a78fbaebe585 to your computer and use it in GitHub Desktop.
Rudimentary parsing of terraform tfvars in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# source: https://gist.github.com/atward/7a2eb1b4a78fbaebe585 | |
# here be dragons: this is as dangerous as it looks | |
## terraform variable defaults | |
# takes *.tf and assigns env=default (if any) | |
# - map not supported (obvious reasons) | |
# - Tested on Darwin sed(1) only | |
function source_tfdefaults() { | |
eval "$( | |
sed -n '/variable [-a-z0-9_]* {/{:start | |
/}/!{N;b start | |
} | |
s/^variable \([-a-z0-9_]*\).*default *= *"\([^"]*\)".*/\1="\2"/p | |
}' "$@" | |
)" | |
} | |
## tfvars assignments | |
# takes *.tfvars and assigns env=value | |
# - works with terraform 0.6 types: string, map | |
# - map.key becomes map_key | |
function source_tfvars() { | |
eval "$( | |
awk 'BEGIN {FS=OFS="="} | |
!/^(#| *$)/ && /^.+=.+$/ { | |
gsub(/^[ \t]+|[ \t]+$/, "", $1); | |
gsub(/\./, "_", $1); | |
gsub(/^[ \t]+|[ \t]+$/, "", $2); | |
if ($1 && $2) print $0 | |
}' "$@" | |
)" | |
} | |
# usage: | |
#source_tfdefaults *.tf | |
#source_tfvars "./conf.tfvars" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you consider using tfvars.json files and then processing them using jq?