Skip to content

Commit

Permalink
Script to provide TF_VARs from yaml
Browse files Browse the repository at this point in the history
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
keymon authored and saliceti committed Feb 29, 2016
1 parent fa735c2 commit 5fbe444
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Empty file modified concourse/scripts/extract_tf_vars_from_terraform_state.rb
100644 → 100755
Empty file.
30 changes: 30 additions & 0 deletions concourse/scripts/extract_tf_vars_from_yaml.rb
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"])

0 comments on commit 5fbe444

Please sign in to comment.