Puppet Function: split_ip_by_version

Defined in:
lib/puppet/parser/functions/split_ip_by_version.rb
Function type:
Ruby 3.x API

Overview

split_ip_by_version()Any

Returns a hash with 3 arrays, one containing IPv4 addresses/subnets, one with only IPv6 addresses/subnets, and one with elements that didn’t match either.

Returns:

  • (Any)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/parser/functions/split_ip_by_version.rb', line 5

newfunction(:split_ip_by_version, :type => :rvalue, :doc => <<-EOS
Returns a hash with 3 arrays, one containing IPv4 addresses/subnets, one with
only IPv6 addresses/subnets, and one with elements that didn't match either.
  EOS
) do |arguments|
  require 'ipaddr'

  # allow them to pass nothing, and we'll return 3 empty arrays
  arguments = [ ] if arguments == nil

  v4_arr = Array.new
  v6_arr = Array.new
  other_arr = Array.new

  arguments.each do |arg|
    # We'll accept an array of strings, or just strings as arguments
    if arg.is_a?(Array)
      arg.each do |element|
        begin
          ip = IPAddr.new(element)
          v4_arr.push(element) if ip.ipv4?
          v6_arr.push(element) if ip.ipv6?
        rescue ArgumentError
          other_arr.push(element)
          next
        end # begin/rescue
      end # array traversal
    elsif arg.is_a?(String)
      # This argument is a string
      arg.split(',').each do |a|
        begin
          ip = IPAddr.new(a)
          v4_arr.push(a) if ip.ipv4?
          v6_arr.push(a) if ip.ipv6?
        rescue
          other_arr.push(arg)
        end
      end
    end # string handling
  end # argument traversal

  ret_hash = { '6' => v6_arr, '4' => v4_arr, 'other' => other_arr }
  return ret_hash
end