Puppet Function: extlib::compare_ip

Defined in:
lib/puppet/functions/extlib/compare_ip.rb
Function type:
Ruby 4.x API

Overview

extlib::compare_ip(Stdlib::IP::Address $left, Stdlib::IP::Address $right)Integer[-1,1]

A function that compares two IP addresses. To be used with the built-in sort() function.

Examples:

Sorting the array of IP addresses

$ip_addresses = ['10.1.1.1', '10.10.1.1', '10.2.1.1']
$ip_addresses.sort |$a, $b| { extlib::compare_ip($a, $b) }

Parameters:

  • left (Stdlib::IP::Address)

    Left value

  • right (Stdlib::IP::Address)

    Right value

Returns:

  • (Integer[-1,1])

    -1, 0 or 1 if left value is lesser, equal or greater than right value



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/puppet/functions/extlib/compare_ip.rb', line 7

Puppet::Functions.create_function(:'extlib::compare_ip') do
  # @param left Left value
  # @param right Right value
  # @return -1, 0 or 1 if left value is lesser, equal or greater than right value
  # @example Sorting the array of IP addresses
  #   $ip_addresses = ['10.1.1.1', '10.10.1.1', '10.2.1.1']
  #   $ip_addresses.sort |$a, $b| { extlib::compare_ip($a, $b) }
  dispatch :compare_ip do
    param 'Stdlib::IP::Address', :left
    param 'Stdlib::IP::Address', :right
    return_type 'Integer[-1,1]'
  end

  def compare_ip(left, right)
    IPAddr.new(left).to_i <=> IPAddr.new(right).to_i
  end
end