I have to write a function that will, in order, perform the following to read the value of a variable:
I have managed to do it in my Puppet script using this if condition.
# foo is read from (in order of preference): facter fact, hiera hash, hard coded value
if $::foo == undef {
$foo = hiera('foo', 'default_value')
} else {
$foo = $::foo
}
But I want to avoid repeating this if condition for each variable I wish to parse in this manner, and therefore thought of writing a new Puppet function of the format get_args('foo', 'default_value')
which will return me the value from
default_value
.I know that I can use lookupvar
to read a facter fact from a ruby function. How do I read a hiera variable from my Puppet ruby function?
You can call defined functions using the function_
prefix.
You have found the lookupvar
function already.
Putting it all together:
module Puppet::Parser::Functions
newfunction(:get_args, :type => :rvalue) do |args|
# retrieve variable with the name of the first argument
variable_value = lookupvar(args[0])
return variable_value if !variable_value.nil?
# otherwise, defer to the hiera function
function_hiera(args)
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With