2011-04-23 02:19:43 +02:00
|
|
|
#
|
|
|
|
# fact.rb
|
|
|
|
#
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
|
|
|
newfunction(:fact, :type => :rvalue, :doc => <<-EOS
|
2011-04-23 02:56:30 +02:00
|
|
|
This function will retrieve fact from Facter based on the fact
|
|
|
|
name and expose it for further use within Puppet manifest file ...
|
2011-04-23 02:19:43 +02:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
Given the following sample manifest:
|
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
define partitions {
|
|
|
|
$result = split(fact("partitions_${name}"), ',')
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
notice $result
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
partition { $result: }
|
|
|
|
}
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
define partition {
|
|
|
|
notice $name
|
|
|
|
}
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
$available_disks = split($disks, ',')
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
partitions { $available_disks: }
|
2011-04-23 02:19:43 +02:00
|
|
|
|
|
|
|
This will produce the following:
|
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
notice: Scope(Partitions[hda]): hda1 hda2
|
|
|
|
notice: Scope(Partition[hda1]): hda1
|
|
|
|
notice: Scope(Partition[hda2]): hda2
|
2011-04-23 02:19:43 +02:00
|
|
|
|
|
|
|
Which allows you to avoid resorting to the following:
|
|
|
|
|
2011-04-23 02:56:30 +02:00
|
|
|
$fact = "partitions_${name}"
|
|
|
|
$result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',')
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-25 03:44:20 +02:00
|
|
|
Phasing out the need for use and abuse of the infamous inline_template
|
|
|
|
in the partitions define given above.
|
2011-04-23 02:19:43 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
|
|
|
|
2011-04-25 03:02:00 +02:00
|
|
|
raise(Puppet::ParseError, "fact(): Wrong number of arguments " +
|
2011-04-23 02:19:43 +02:00
|
|
|
"given (#{arguments.size} for 1)") if arguments.size < 1
|
|
|
|
|
|
|
|
fact = arguments[0]
|
|
|
|
|
2011-04-25 03:02:00 +02:00
|
|
|
raise(Puppet::ParseError, 'fact(): You must provide ' +
|
|
|
|
'fact name') if fact.empty?
|
2011-04-23 02:19:43 +02:00
|
|
|
|
2011-04-25 03:02:00 +02:00
|
|
|
fact = strinterp(fact) # Evaluate any interpolated variable names ...
|
2011-04-23 02:19:43 +02:00
|
|
|
result = lookupvar(fact) # Get the value of interest from Facter ...
|
|
|
|
|
|
|
|
if not result or result.empty?
|
2011-04-25 03:02:00 +02:00
|
|
|
#
|
|
|
|
# Now this is a funny one ... Puppet does not have a concept of
|
|
|
|
# returning neither undef nor nil back for use within the Puppet DSL
|
|
|
|
# and empty string is as closest to actual undef as you we can get
|
|
|
|
# at this point in time ...
|
|
|
|
#
|
|
|
|
result = ''
|
2011-04-23 02:19:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|