I have to write a function that will, in order, perform the following to read the value of a variable:
- Check if there is a facter variable defined. If not,
- Read the value of the variable from Hiera. If not,
- Use a default value.
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
- a facter fact if it exists,
- a hiera variable, or
- just return
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?