I would like to suggest that you
not do this via a fact. More generally, I consider it a principle of a good design to avoid creating any fact that is strictly derivative of other facts.
You can get a top scope variable with the same value in several other ways, principal among them:
- Compute it directly at top scope in your site manifest
- Compute it in a class that manages no resources, 'include' that class at top scope, and set the top-scope variable from the class variable
As a subset of (1), you could consider creating and using a custom function, which would be very clean as far as your manifests go.
Note, however, that for
most purposes you don't actually need a top-scope variable; you could instead use a class variable directly. Either way, you should be using a fully-qualified name everywhere you refer to the variable in your manifests, so the choice of namespace is mostly a personal preference in that context. The only use I can think of where you actually need a top-scope variable is if you want to interpolate it into Hiera hierarchy definitions (which is indeed a perfectly reasonable thing to do).
Option 2 might look like this:
modules/site/manifests/hostname_info.pp:
----
class site::hostname_info {
$hostname_parts = split($::hostname, '-')
$network = $hostname_parts[0]
$role = $hostname_parts[1]
$number = $hostname_parts[2]
}
manifests/site.pp (or any other manifest where you need the info):
----
# ...
include 'site::hostname_info'
$host_network = $site::hostname_info::network
# ...
John