There are some important question to answer here to determine the best course of action:
- why are the values for foo::param and bar::param the same? If it is circumstantial or coincidental, then squeezing out the duplication is probably harmful rather than helpful. In that case I would recommend just having duplicate data
- Supposing that there is a functional reason why the two parameters are the same, the next question is who owns the data? If one of the classes is the logical owner, then it is right for the other to depend on it.
If the data are fundamentally the same, but neither class can reasonably be construed as its owner, then it ought to be given to some third entity on which both the others can rely. That "entity" might be an actual class, or it might just be a separate logical namespace for the Hiera key.
What I would like to do is making hiera resolves "reference" and
automatically retrieve foo::param value, even if foo class is not included.
I think that's a bad idea because it hides the logical relationship among your classes in your data instead of making it evident in your classes.
If you insist on pursuing such a course, however, then you should investigate whether the underlying YAML parser supports YAML 1.2. That version of YAML has a built-in mechanism for aliasing data elsewhere in a document, which should accomplish what you seek. Think again, though.
Use case could be to define some global value like web application
parameters like hostname which should be defined globally and used when
needed.
That sort of thing really ought to be pulled out to its own class. Example:
class webapp::server {
# Note: hides the $::hostname fact
$hostname = hiera('webapp::server::hostname')
}
class webapp::app1 {
include 'webapp::server'
# use $webapp::server::hostname ...
}
class webapp::app2 {
include 'webapp::server'
# use $webapp::server::hostname ...
}
John