diff --git a/site-modules/profile/lib/puppet/functions/ip_for_network.rb b/site-modules/profile/lib/puppet/functions/ip_for_network.rb new file mode 100644 index 00000000..f76431d9 --- /dev/null +++ b/site-modules/profile/lib/puppet/functions/ip_for_network.rb @@ -0,0 +1,39 @@ +require "ipaddr" + +Puppet::Functions.create_function(:ip_for_network) do + dispatch :get_ip do + param 'String', :range + end + + def get_ip(range) + addresses_in_range = [] + ip_addresses = [] + interfaces = [] + + range = IPAddr.new(range) + scope = closure_scope + interfaces = scope['facts']['networking']['interfaces'] + interfaces.each do |name, data| + bindings = data['bindings'] + unless bindings.nil? + bindings.each do |binding_| + ip = binding_['address'] + unless ip.nil? + ip_addresses.push(ip) + end + end + end + end + + ip_addresses.each do |string_address| + ip_address = IPAddr.new(string_address) + if range.include?(ip_address) + addresses_in_range.push(string_address) + end + end + + # TODO don't be a dork dork with the return + # handle multiple values! + return addresses_in_range.first + end +end diff --git a/site-modules/profile/lib/puppet/parser/functions/ip_for_network.rb b/site-modules/profile/lib/puppet/parser/functions/ip_for_network.rb deleted file mode 100644 index 011e486a..00000000 --- a/site-modules/profile/lib/puppet/parser/functions/ip_for_network.rb +++ /dev/null @@ -1,35 +0,0 @@ -require "ipaddr" - -module Puppet::Parser::Functions - - newfunction(:ip_for_network, :type => :rvalue, :doc => <<-EOS -Returns an ip address for the given network in cidr notation - -ip_for_network("127.0.0.0/24") => 127.0.0.1 - EOS - ) do |args| - addresses_in_range = [] - ip_addresses = [] - interfaces = [] - - range = IPAddr.new(args[0]) - interfaces = lookupvar('interfaces').split(",") - interfaces.each do |i| - ip = lookupvar("ipaddress_#{i}") - unless ip.nil? - ip_addresses.push(ip) - end - end - - ip_addresses.each do |string_address| - ip_address = IPAddr.new(string_address) - if range.include?(ip_address) - addresses_in_range.push(string_address) - end - end - - # TODO don't be a dork dork with the return - # handle multiple values! - return addresses_in_range.first - end -end