Question

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?

Was it helpful?

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"

OTHER TIPS

You could evaluate double substitution with a inline_template:

class servers::something::something2(
  $query_cache_type = "Off",
  ) {
    $params_file = "servers::something::${::env}"
    include "${params_file}"
    $rp1 = inline_template("<%= scope.lookupvar('${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}": }

}

But for this use case, it seems more appropiate to use hiera to keep environment configuration

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top