define location ($host) {notify {"Location being defined ${title} -> ${host}": }}# export the locationnode 'a' {@@location { "mysql-server":host => $hostname}}node 'b' {# later import it on another nodeLocation <<| title == "mysql-server" |>># extract the data$mysql_server_host = getparam(Location["mysql-server"], "host")}
class class_example() {# something like realize Location["mysql-server"] doesn't even compileLocation <<| title == "mysql-server" |>>$mysql_server_host = getparam(Location["mysql-server"], "host")notify {"Example in class: ${mysql_server_host}": }}node 'b' {class { class_example: }}
Yields: "Notice: Example in class:"define resource_example() {Location <<| title == "mysql-server" |>>$mysql_server_host = getparam(Location["mysql-server"], "host")notify {"Example in resource: ${mysql_server_host}": }}
Yields "Notice: Example in resource: hostname-x"
As far as I understand this is not really possible. This is because you don’t have control over the order of compilation of resources into the catalog. The compiler first evaluates all classes then moves on and eventually has a step that evaluates all collections and so on until the catalog is complete.
You can work around this by ‘bumping scope’ which means you need to get the evaluation of the getparam() to happen after the collection has occurred and the resources are in the catalog. This is essentially what is happening in your define example.
By bumping scope I mean that you need to do any evaluations in a define that is ‘one scope’ more then then where then resources will be added to the catalog. My scope I mean the order that the compiler will evaluate things.
So when you have a collection in a class the actual collection will happen after all classes are evaluated by the initial pass by the compiler hence why you see the behavior you do. So if you create a define, use it in the class and have that define evaluate the getparam() then it /might/ work. Of course you won’t be able to get to those values in your class (because its already been evaluated). I say /might/ work because you can’t be sure that the define you create and the defined type you collected will happen in the right order ie that the collection will happen first. To be sure you can create a second define and use that in the first define and move the evaluation to the new second define. This is why I call this ‘bumping scope’. At this point the evaluation will happen after the collection’s defined types are in the catalog and getparam() will work. This get fun when defines make use of defines and the other defines need to reference these 2nd level (or 3rd level) defines. You can keep adding defines to get things to work.
Now this looks ugly and is and I feel bad that I’ve done this but it works well and has predictable results.
Finally the only way I think this can be fixed is it puppet went to a multipass compiler that would ‘reevaluate’ resources if evaluations occurred that reference those resources or their parameters. This would slow down performance quite and bit and have almost no benefit for most people.
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/e331a5fc-657a-47ae-aa0d-64f2a655ff10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.