Last active
August 29, 2015 13:56
-
-
Save jtopjian/8799635 to your computer and use it in GitHub Desktop.
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
| require 'hiera' | |
| class CapHiera | |
| def self.hiera key, options = {} | |
| scope = {} | |
| if options[:host] | |
| scope[:host] = options[:host] | |
| scope[:role] = options[:host].roles.to_a | |
| end | |
| if options[:stage] | |
| scope[:stage] = options[:stage] | |
| else | |
| scope[:stage] = fetch(:stage) | |
| end | |
| begin | |
| @@h ||= Hiera.new({:config => './hiera/hiera.yaml'}) | |
| rescue Exception => e | |
| STDERR.puts "Failed to start Hiera: #{e.class}: #{e}" | |
| exit 1 | |
| end | |
| @@h.lookup(key, 'nil', scope, order_override=nil, resolution_type=:priority) | |
| end | |
| def self.build_servers_from_stage stage | |
| servers = CapHiera.hiera('servers', {:stage => stage}) | |
| servers.each do |k,v| | |
| server k, v | |
| end | |
| end | |
| end |
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
| require_relative 'lib/capistrano/cap_hiera' | |
| CapHiera.build_servers_from_stage 'staging' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How did you rewrite this into a module?
EDIT: Found it! https://github.com/jtopjian/capistrano-tutorial/blob/stage3/modules/hiera/lib/cap_hiera.rb
Thanks @jtopjian !