How to find out the value of Hiera from the Puppet function?

I need to write a function that will, in order to do the following, to read the value of a variable:

  • Check if a specific facter variable exists. If not,
  • Read the value of the variable from Hiera. If not,
  • Use the default value.

I managed to do this 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 every variable that I want to analyze in this way, and therefore thought about writing a new Puppet function of the format get_args('foo', 'default_value') , which will return me the value from

  • fact of fact, if it exists,
  • hiera variable or
  • just return default_value .

I know that I can use lookupvar to read the fact from the ruby ​​function. How to read hiera variable from my ruby ​​Puppet function?

+5
source share
1 answer

You can call certain functions using the function_ prefix.

You have already found the lookupvar function.

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 
+3
source

Source: https://habr.com/ru/post/1200404/


All Articles