Search code examples
puppet

Dynamic variable as part of class name


I'm trying to do dynamic inclusion of a class based on a variable, which works fine, but then trying to lookup a variable within that dynamically included class doesn't.

class servers::something::something2(
  $query_cache_type = "Off",
  ) {
    $params_file = "servers::something::${::env}"
    include "${params_file}"
    $rp1 = "${params_file}::root_passwd"
    $rp2 = $servers::something::dev::root_passwd
    notify{"The value is: ${params_file}": }
    notify{"The value is: ${rp1}": }
    notify{"The value is: ${rp2}": }

}

Gives me output like this:

Notice: The value is: servers::mysql::dev
Notice: The value is: servers::mysql::dev::root_passwd
Notice: The value is: some_pass

How do I get rp1, the dynamic lookup of the value, to work correctly?


Solution

  • Instead of using inline template it's much better to install Puppetlab's stdlib module which provides among other things a lot of excellent functions, like getvar.

    Once you have done this you can simply write

    $rp1 = getvar("${params_file}::root_passwd")
    

    and you are done. Here is an example:

    class x::y {
      $z = "hello world"
    }
    include x::y    
    $i = "x::y"
    alert( getvar ( "${i}::z" ) ) # outputs "hello world"