| OLD | NEW |
| 1 #!/usr/bin/ruby | 1 #!/usr/bin/ruby |
| 2 # This script is a node classifier for Puppet that operates on top of Hiera | 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. | 3 # and uses a custom hosts.yaml config to map host roles. |
| 4 | 4 |
| 5 require 'getoptlong' | 5 require 'getoptlong' |
| 6 require 'hiera' | 6 require 'hiera' |
| 7 require 'socket' | 7 require 'socket' |
| 8 require 'yaml' | 8 require 'yaml' |
| 9 | 9 |
| 10 # Where to search for the Hiera configuration | 10 # Where to search for the Hiera configuration |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 #{BASENAME}: unknown option: #{ARGV[0]} | 57 #{BASENAME}: unknown option: #{ARGV[0]} |
| 58 #{BASENAME}: try #{BASENAME} --help | 58 #{BASENAME}: try #{BASENAME} --help |
| 59 END | 59 END |
| 60 exit 1 | 60 exit 1 |
| 61 end | 61 end |
| 62 | 62 |
| 63 # Extract the server -> hostname -> role information from the hosts | 63 # Extract the server -> hostname -> role information from the hosts |
| 64 # configuration file: | 64 # configuration file: |
| 65 begin | 65 begin |
| 66 config = YAML.load_file(HOSTS_CONFIG) | 66 config = YAML.load_file(HOSTS_CONFIG) |
| 67 servers = config.fetch("servers", {}) | 67 servers = config.fetch("adblockplus::hosts", {}) |
| 68 host = servers.fetch(hostname, {}) | 68 host = servers.fetch(hostname, {}) |
| 69 role = host.fetch("role", "default") | 69 role = host.fetch("role", "default") |
| 70 rescue Exception => error | 70 rescue Exception => error |
| 71 STDERR.puts "#{BASENAME}: #{error.message}: #{HOSTS_CONFIG}" | 71 STDERR.puts "#{BASENAME}: #{error.message}: #{HOSTS_CONFIG}" |
| 72 exit 1 | 72 exit 1 |
| 73 end | 73 end |
| 74 | 74 |
| 75 # Map Hiera data into the structure Puppet expects an ENC to generate (see | 75 # Map Hiera data into the structure Puppet expects an ENC to generate (see |
| 76 # https://docs.puppetlabs.com/guides/external_nodes.html for more info): | 76 # https://docs.puppetlabs.com/guides/external_nodes.html for more info): |
| 77 begin | 77 begin |
| 78 hiera = Hiera.new(:config => HIERA_CONFIG) | 78 hiera = Hiera.new(:config => HIERA_CONFIG) |
| 79 scope = {'::hostname' => hostname, '::role' => role} | 79 scope = {'::hostname' => hostname, '::role' => role} |
| 80 classes = hiera.lookup('classes', {}, scope, nil, :hash) | 80 classes = hiera.lookup('classes', {}, scope, nil, :hash) |
| 81 parameters = hiera.lookup('parameters', {}, scope, nil, :hash) | 81 parameters = hiera.lookup('parameters', {}, scope, nil, :hash) |
| 82 parameters['role'] = role | 82 parameters['role'] = role |
| 83 result = { 'classes' => classes, 'parameters' => parameters } | 83 result = { 'classes' => classes, 'parameters' => parameters } |
| 84 rescue Exception => error | 84 rescue Exception => error |
| 85 STDERR.puts "#{BASENAME}: #{error.message}: #{HIERA_CONFIG}" | 85 STDERR.puts "#{BASENAME}: #{error.message}: #{HIERA_CONFIG}" |
| 86 exit 1 | 86 exit 1 |
| 87 end | 87 end |
| 88 | 88 |
| 89 puts result.to_yaml | 89 puts result.to_yaml |
| 90 | 90 |
| OLD | NEW |