Small re-factor of fact function.
Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>
This commit is contained in:
parent
7e7c7ce2ee
commit
50cb2cd05a
1 changed files with 11 additions and 45 deletions
56
fact.rb
56
fact.rb
|
@ -4,42 +4,6 @@
|
|||
|
||||
module Puppet::Parser::Functions
|
||||
newfunction(:fact, :type => :rvalue, :doc => <<-EOS
|
||||
This function will retrieve fact from Facter based on the fact
|
||||
name and expose it for further use within Puppet manifest file ...
|
||||
|
||||
For example:
|
||||
|
||||
Given the following sample manifest:
|
||||
|
||||
define partitions {
|
||||
$result = split(fact("partitions_${name}"), ',')
|
||||
|
||||
notice $result
|
||||
|
||||
partition { $result: }
|
||||
}
|
||||
|
||||
define partition {
|
||||
notice $name
|
||||
}
|
||||
|
||||
$available_disks = split($disks, ',')
|
||||
|
||||
partitions { $available_disks: }
|
||||
|
||||
This will produce the following:
|
||||
|
||||
notice: Scope(Partitions[hda]): hda1 hda2
|
||||
notice: Scope(Partition[hda1]): hda1
|
||||
notice: Scope(Partition[hda2]): hda2
|
||||
|
||||
Which allows you to avoid resorting to the following:
|
||||
|
||||
$fact = "partitions_${name}"
|
||||
$result = split(inline_template("<%= scope.lookupvar(fact) %>"), ',')
|
||||
|
||||
Phasing out the need for use and abuse of the infamous inline_template
|
||||
in the partitions define given above.
|
||||
EOS
|
||||
) do |arguments|
|
||||
|
||||
|
@ -54,18 +18,20 @@ in the partitions define given above.
|
|||
fact = strinterp(fact) # Evaluate any interpolated variable names ...
|
||||
result = lookupvar(fact) # Get the value of interest from Facter ...
|
||||
|
||||
if not result or result.empty?
|
||||
#
|
||||
# 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 = ''
|
||||
end
|
||||
#
|
||||
# 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 = (not result or result.empty?) ? '' : result
|
||||
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
# vim: set ts=2 sw=2 et :
|
||||
|
||||
notice fact('interfaces')
|
||||
notice fact('xyz')
|
||||
notice fact('')
|
||||
|
|
Loading…
Reference in a new issue