Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic variable as part of class name

Tags:

puppet

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?

like image 749
Gabriel Baker Avatar asked Dec 12 '25 11:12

Gabriel Baker


1 Answers

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"
like image 191
Lodewijk Bogaards Avatar answered Dec 15 '25 19:12

Lodewijk Bogaards



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!