Skip to content

Instantly share code, notes, and snippets.

@natemccurdy
Last active December 20, 2017 20:47
Show Gist options
  • Save natemccurdy/ae49d2929111a68f0e78d39e75f1c8a1 to your computer and use it in GitHub Desktop.
Save natemccurdy/ae49d2929111a68f0e78d39e75f1c8a1 to your computer and use it in GitHub Desktop.
Script to return list of masters. Useful in Bolt, for loops, and scripting
#!/usr/bin/env ruby
require 'yaml'
require 'optparse'
options = {
file: File.expand_path('masters.yaml'),
type: nil,
environment: nil
}
ARGV.options do |opts|
opts.banner = 'Usage: masters [options]'
opts.on('--file SERVER_LIST') { |val| options[:file] = val }
opts.on('--environment x,y,z', Array) { |val| options[:environment] = val }
opts.on('--type TYPE', %w[mom com]) { |val| options[:type] = val }
begin
opts.parse!
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument => e
STDERR.puts e
exit 1
end
end
if File.exist?(options[:file])
masters = YAML.load_file(options[:file])
else
STDERR.puts "File does not exist: #{options[:file]}"
exit 1
end
masters.each do |environment, types|
# Skip the environment if it's not in the list of environments requested.
next if options[:environment] && !options[:environment].include?(environment)
types.each do |type, servers|
# Skip the type (mom or com) if it's not the type requested.
next if options[:type] && type != options[:type]
puts servers
end
end
# environment: top-level key
# type: mom (masters of masters) or com (compile master)
---
production:
mom: puppet1001.foo.org
com:
- puppet1029.foo.org
- puppet1042.foo.org
- puppet1012.foo.org
- puppet1043.foo.org
qa:
mom: non-prod-pup0013.lab.foo.org
com:
- non-prod-pup0012.lab.foo.org
- non-prod-pup0014.lab.foo.org
dev:
mom: non-prod-pup0011.lab.foo.org
com:
- non-prod-pup0010.lab.foo.org
- non-prod-pup0010.lab.foo.org
@natemccurdy
Copy link
Author

Example usage:

show masters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment