Skip to content

Instantly share code, notes, and snippets.

@vjanelle
Last active December 14, 2015 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjanelle/5061070 to your computer and use it in GitHub Desktop.
Save vjanelle/5061070 to your computer and use it in GitHub Desktop.
attempts to find an interface that'll serve the IP address
# find_ip_by_interface.rb
#
# Attempts to discover which interface will serve a particular destination
#
require 'ipaddr'
module Puppet::Parser::Functions
newfunction(:find_ip_by_interface, :type => :rvalue) do |args|
address = IPAddr.new(args[0])
retcon = ""
lookupvar('interfaces').split(',').each do |interface|
netmask = lookupvar("netmask_#{interface}")
int_addr = lookupvar("ipaddress_#{interface}")
int = IPAddr.new("#{int_addr}/#{netmask}")
if int.include?(address)
retcon = interface
end
end
retcon
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment