From b0a64a6d20bae9ff569ff21b72ced71b6796ebeb Mon Sep 17 00:00:00 2001 From: Steffen Zieger Date: Fri, 23 Nov 2012 17:41:45 +0100 Subject: [PATCH] add custom function which returns all ipaddresses as an array --- lib/puppet/parser/functions/ipaddresses.rb | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 lib/puppet/parser/functions/ipaddresses.rb diff --git a/lib/puppet/parser/functions/ipaddresses.rb b/lib/puppet/parser/functions/ipaddresses.rb new file mode 100644 index 000000000..98a29a3c6 --- /dev/null +++ b/lib/puppet/parser/functions/ipaddresses.rb @@ -0,0 +1,40 @@ +module Puppet::Parser::Functions + newfunction(:ipaddresses, :type => :rvalue, :doc => <<-EOS +Returns all ip addresses of network interfaces (except lo) found by facter. +EOS + ) do |args| + interfaces = lookupvar('interfaces') + + return false if (interfaces == :undefined) + + result = [] + if interfaces.count(',') > 0 + interfaces = interfaces.split(',') + interfaces.each do |iface| + if ! iface.include?('lo') + ipaddr = lookupvar("ipaddress_#{iface}") + ipaddr6 = lookupvar("ipaddress6_#{iface}") + if ipaddr + result << ipaddr + end + if ipaddr6 + result << ipaddr6 + end + end + end + else + if ! interfaces.include?('lo') + ipaddr = lookupvar("ipaddress_#{interfaces}") + ipaddr6 = lookupvar("ipaddress6_#{interfaces}") + if ipaddr + result << ipaddr + end + if ipaddr6 + result << ipaddr6 + end + end + end + + return result + end +end