From 1bd7a12545f37e9d075e6cad9503172f174fd91f Mon Sep 17 00:00:00 2001 From: Stephen Gran Date: Sat, 14 Nov 2009 20:51:23 +0000 Subject: [PATCH] Add a method to get all the yaml stored (local) info for all hosts Signed-off-by: Stephen Gran --- .../lib/puppet/parser/functions/nodeinfo.rb | 40 +----------- .../lib/puppet/parser/functions/yamlinfo.rb | 63 +++++++++++++++++++ 2 files changed, 65 insertions(+), 38 deletions(-) create mode 100644 files/etc/puppet/lib/puppet/parser/functions/yamlinfo.rb diff --git a/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb b/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb index e9b72fa9..fae9d6dd 100644 --- a/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb +++ b/files/etc/puppet/lib/puppet/parser/functions/nodeinfo.rb @@ -3,47 +3,11 @@ module Puppet::Parser::Functions host = args[0] yamlfile = args[1] - parser.watch_file(yamlfile) - require 'yaml' require '/etc/puppet/lib/puppet/parser/functions/ldapinfo.rb' + require '/etc/puppet/lib/puppet/parser/functions/yamlinfo.rb' - $KCODE = 'utf-8' - - yaml = YAML.load_file(yamlfile) - results = {} - - ['nameinfo', 'footer'].each do |detail| - if yaml.has_key?(detail) - if yaml[detail].has_key?(host) - results[detail] = yaml[detail][host] - end - end - end - - if yaml.has_key?('services') - yaml['services'].each_pair do |service, hostlist| - hostlist=[hostlist] unless hostlist.kind_of?(Array) - results[service] = hostlist.include?(host) - end - end - - results['mail_port'] = '' - results['smarthost'] = '' - results['heavy_exim'] = '' - results['smarthost_port'] = 587 - results['reservedaddrs'] = '0.0.0.0/8 : 127.0.0.0/8 : 10.0.0.0/8 : 169.254.0.0/16 : 172.16.0.0/12 : 192.0.0.0/17 : 192.168.0.0/16 : 224.0.0.0/4 : 240.0.0.0/5 : 248.0.0.0/5' - - if yaml['host_settings'].kind_of?(Hash) - yaml['host_settings'].each_pair do |property, values| - if values.kind_of?(Hash) - results[property] = values[host] if values.has_key?(host) - elsif values.kind_of?(Array) - results[property] = "true" if values.include?(host) - end - end - end - + results = function_yamlinfo(host, yamlfile) results['ldap'] = function_ldapinfo(host, '*') return(results) end diff --git a/files/etc/puppet/lib/puppet/parser/functions/yamlinfo.rb b/files/etc/puppet/lib/puppet/parser/functions/yamlinfo.rb new file mode 100644 index 00000000..2cb1f460 --- /dev/null +++ b/files/etc/puppet/lib/puppet/parser/functions/yamlinfo.rb @@ -0,0 +1,63 @@ +module Puppet::Parser::Functions + newfunction(:yamlinfo, :type => :rvalue) do |args| + + host = args[0] + yamlfile = args[1] + parser.watch_file(yamlfile) + + def read_yaml(yaml, host) + results = {} + + ['nameinfo', 'footer'].each do |detail| + if yaml.has_key?(detail) + if yaml[detail].has_key?(host) + results[detail] = yaml[detail][host] + end + end + end + + if yaml.has_key?('services') + yaml['services'].each_pair do |service, hostlist| + hostlist=[hostlist] unless hostlist.kind_of?(Array) + results[service] = hostlist.include?(host) + end + end + + results['mail_port'] = '' + results['smarthost'] = '' + results['heavy_exim'] = '' + results['smarthost_port'] = 587 + results['reservedaddrs'] = '0.0.0.0/8 : 127.0.0.0/8 : 10.0.0.0/8 : 169.254.0.0/16 : 172.16.0.0/12 : 192.0.0.0/17 : 192.168.0.0/16 : 224.0.0.0/4 : 240.0.0.0/5 : 248.0.0.0/5' + + if yaml['host_settings'].kind_of?(Hash) + yaml['host_settings'].each_pair do |property, values| + if values.kind_of?(Hash) + results[property] = values[host] if values.has_key?(host) + elsif values.kind_of?(Array) + results[property] = "true" if values.include?(host) + end + end + end + return(results) + end + + require 'yaml' + $KCODE = 'utf-8' + + yaml = YAML.load_file(yamlfile) + ret = {} + + if host == '*' + Dir.entries('/var/lib/puppet/yaml/node/').each do |fname| + next unless fname =~ /(.*)\.yaml$/ + host_name = $1 + ret[host_name] = read_yaml(yaml, host_name) + end + else + ret = read_yaml(yaml, host) + end + + return(ret) + end +end + -- 2.39.2