Puppet Function: :'stdlib::ip_in_range'

Defined in:
modules/stdlib/lib/puppet/functions/stdlib/ip_in_range.rb
Function type:
Ruby 4.x API

Overview

:'stdlib::ip_in_range'(String $ipaddress, Variant[String, Array] $range)Boolean

Examples:

ip_in_range(<IPv4 Address>, <IPv4 CIDR>)

stdlib::ip_in_range('10.10.10.53', '10.10.10.0/24') => true

Parameters:

  • ipaddress (String)

    The IP address to check

  • range (Variant[String, Array])

    One CIDR or an array of CIDRs defining the range(s) to check against

Returns:

  • (Boolean)

    True or False



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
# File 'modules/stdlib/lib/puppet/functions/stdlib/ip_in_range.rb', line 8

Puppet::Functions.create_function(:'stdlib::ip_in_range') do
  # @param ipaddress The IP address to check
  # @param range One CIDR or an array of CIDRs
  #   defining the range(s) to check against
  #
  # @return [Boolean] True or False
  dispatch :ip_in_range do
    param 'String', :ipaddress
    param 'Variant[String, Array]', :range
    return_type 'Boolean'
  end

  require 'ipaddr'
  def ip_in_range(ipaddress, range)
    ip = IPAddr.new(ipaddress)

    if range.is_a? Array
      ranges = range.map { |r| IPAddr.new(r) }
      ranges.any? { |rng| rng.include?(ip) }
    elsif range.is_a? String
      ranges = IPAddr.new(range)
      ranges.include?(ip)
    end
  end
end