Puppet Function: is_ipv4

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

Overview

is_ipv4()Any

Given a string, returns true if it is a valid ipv4 address or subnet

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
# File 'lib/puppet/parser/functions/is_ipv4.rb', line 5

newfunction(:is_ipv4, :type => :rvalue, :doc => <<-EOS
Given a string, returns true if it is a valid ipv4 address or subnet
  EOS
) do |arguments|

  # Verify we've got the right number of arguments. This is the only error
  # we should be throwing
  if arguments.size != 1
    raise( Puppet::ParseError, "is_ipv4(): wrong number of arguments " +
          "given. #{arguments.size} for 1" )
  end
  input = arguments[0]

  require 'ipaddr'

  begin
    addr = IPAddr.new(input)
  rescue
    return false
  end

  return true if addr.ipv4?
  return false
end