-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Semi generic script that reads a YAML tree and creates a bash script to export the data as variables. They are prefixed with *TF_VAR_* to be able to read them directly in Terraform.
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env ruby | ||
# | ||
# Converts a YAML to a bunch of bash variable assignations. | ||
# - It will concatenate the key tree using '_' until it finds a basic value | ||
# - for lists, if the element is a hash and has a key 'name', uses that as | ||
# a node name, if not, uses the index. | ||
|
||
require 'yaml' | ||
|
||
def process_yaml(yaml_tree, prefix_chain) | ||
case yaml_tree | ||
when Hash | ||
yaml_tree.each { |k,v| | ||
process_yaml(v, prefix_chain + [k]) | ||
} | ||
when Array | ||
yaml_tree.each_with_index { |v,i| | ||
if v.instance_of? Hash and v['name'] | ||
name = v['name'] | ||
else | ||
name = i | ||
end | ||
process_yaml(v, prefix_chain + [name]) | ||
} | ||
else | ||
puts "#{prefix_chain.join('_')}='#{yaml_tree}'" | ||
end | ||
end | ||
|
||
process_yaml(YAML.load($stdin), ["export TF_VAR"]) |