Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env ruby | |
2 # This script is a node classifier for Puppet that operates on top of Hiera | |
3 # and uses a custom hosts.yaml config to map host roles. | |
4 | |
5 require 'getoptlong' | |
6 require 'hiera' | |
7 require 'socket' | |
8 require 'yaml' | |
9 | |
10 # Where to search for the Hiera configuration | |
11 HIERA_CONFIG = ENV.fetch('PUPPET_HIERA_CONFIG', '/etc/puppet/hiera.yaml') | |
12 # Where to search for the Hosts configuration | |
13 HOSTS_CONFIG = ENV.fetch('PUPPET_HOSTS_CONFIG', '/etc/puppet/infrastructure/hier a/environment/hosts.yaml') | |
Wladimir Palant
2014/11/17 16:43:36
I don't think hardcoding /etc/puppet/infrastructur
mathias
2014/11/27 00:30:18
It would work in this case, indeed. Yet I don't be
| |
14 | |
15 # The current script name, used for logging and usage hints | |
16 BASENAME = File.basename($0) | |
17 | |
18 # Although there's no need for any options in particular yet, the script | |
19 # still recognizes a --help option, which is considered the bare minimum by | |
20 # convention. Nonetheless, future options may become integrated here (see | |
21 # also http://ruby-doc.org/stdlib/libdoc/getoptlong/rdoc/GetoptLong.html): | |
22 GetoptLong.new( | |
23 ['--help', '-h', GetoptLong::NO_ARGUMENT] | |
24 ).each do |opt, arg| | |
25 case opt | |
26 | |
27 when '--help' | |
28 puts <<-END | |
29 Usage: #{BASENAME} [hostname] | |
30 #{BASENAME} example.com | |
31 #{BASENAME} --help | |
32 | |
33 Options: | |
34 | |
35 --help, -h | |
36 Display this help message and exit gracefully. | |
37 | |
38 Environment: | |
39 | |
40 PUPPET_HIERA_CONFIG=#{HIERA_CONFIG} | |
41 Where to find the hiera configuration file. | |
42 PUPPET_HOSTS_CONFIG=#{HOSTS_CONFIG} | |
43 Where to find the hosts configuration file. | |
44 | |
45 END | |
46 exit 0 | |
47 | |
48 end | |
49 end | |
50 | |
51 # Only one additional non-option argument is allowed, in order to explicitly | |
52 # specify a hostname to use instead of the default: | |
53 case ARGV.length | |
54 when 0 | |
55 hostname = Socket.gethostname | |
56 when 1 | |
57 hostname = ARGV[0][/^[^.]+/] | |
58 else | |
59 STDERR.puts <<-END | |
60 #{BASENAME}: unknown option: #{ARGV[0]} | |
61 #{BASENAME}: try #{BASENAME} --help | |
62 END | |
63 exit 1 | |
64 end | |
65 | |
66 # Extract the server -> hostname -> role information from the hosts | |
67 # configuration file: | |
68 begin | |
69 config = YAML.load_file(HOSTS_CONFIG) | |
70 servers = config.fetch("servers", {}) | |
71 host = servers.fetch(hostname, {}) | |
72 role = host.fetch("role", "default") | |
73 rescue Exception => error | |
74 STDERR.puts "#{BASENAME}: #{error.message}: #{HOSTS_CONFIG}" | |
75 exit 1 | |
76 end | |
77 | |
78 # Map Hiera data into the structure Puppet expects an ENC to generate (see | |
79 # https://docs.puppetlabs.com/guides/external_nodes.html for more info): | |
80 begin | |
81 hiera = Hiera.new(:config => HIERA_CONFIG) | |
82 scope = {'::hostname'=> hostname, '::role'=> role} | |
83 result = { | |
84 'classes'=> hiera.lookup('classes', {}, scope, nil, :hash), | |
85 'parameters'=> hiera.lookup('parameters', {}, scope, nil, :hash), | |
86 } | |
87 rescue Exception => error | |
88 STDERR.puts "#{BASENAME}: #{error.message}: #{HIERA_CONFIG}" | |
89 exit 1 | |
90 end | |
91 | |
92 puts result.to_yaml | |
93 | |
OLD | NEW |