Search code examples
puppethiera

Pass variable from a puppet class to a hiera .yaml file


does anyone know if a variable can be passed from a puppet class to a .yaml file?

class my_module::my_class () {

  $current_date = '2021-10-12';
}

The objective would be to be able to obtain the value of the variable in a .yaml file

my_module::my_class::date_from_class:

I don't know if this is possible in any way. Thanks in advance


Solution

  • It is possible to interpolate the value of a class variable into your Hiera data:

    my_module::my_class::date_from_class: %{my_module::my_class::current_date}
    

    But it is usually unwise to do so because class variables do not receive their values until and unless the class is evaluated. Class evaluation order is difficult to predict, and may change as the manifest set or class parameter values change.

    If another class wants the value in question then it can just access the class variable directly. Although that's subject to the same caveat, at least another class has the power to ensure that the variable's host class gets evaluated before the variable is accessed:

    class my_module::another_class {
      include my_module::my_class
      $the_date = $my_module::my_class::current_date
    }