I have a bunch of variables that are used in several classes and
definitions, all part of the same module.
As these variables are defined automatically based on system facts, I
would like to avoid having to declare them in each node that uses
classes or definitions from my module.
Where would be the best place to put these variables ? I tried in
<module>/manifests/init.pp as well as in an external file imported from
within classes/definitions files but none work as I would have expected.
Thanks for any hint !
Marc
That's a discussion I had several weeks ago on IRC, but I can't
remember with whom. IIRC, it basically makes a difference wheter the
module is autoloaded or imported. My tests so far have shown that
"module-scope" variables, i.e. e.g. such ones defined at the beginning
of init.pp, get included if you import the module, but not if some
class in the module is autoloaded. I think we had concluded that it
would be nice to have "module-scope" variables in autoloaded classes
and definitions, but had not followed the thought or the tests any
further.
I hope this answers some of your questions, and we can find a solution
that suits everyone. Greetings,
Felix Schäfer
Thank you Felix for this idea ! It indeed helped my classes work
without having to set variables in the global scope.
For future reference, here's the idea:
modules/whatever/manifests/init.pp:
case $operatingsystem {
RedHat: {
$wwwuser = "apache"
}
Debian: {
$wwwuser = "www-data"
}
}
import "classes/*.pp"
import "definitions/*.pp"
In modules/whatever/manifests/*/*.pp I have stuff such as:
class wwwuser {
user { "$wwwuser":
ensure => present,
}
}
And finally in my nodes I do:
import "whatever"
include wwwuser
This solution is convenient enough for my needs. But maybe should we
report this as a feature request to puppet developers ?
Does someone know if there is a good reason variables in init.pp
aren't available in autoloaded classes ?
If this is finally the designed behaviour I will put a note somewhere in
the wiki about the workaround.
Marc
Puppet doesn't actually currently have any concept of module-scoped
variables. Really, the language doesn't even know anything about
modules -- they're convenient ways of organizing files, but they don't
affect things like variable scope.
What you're seeing is a difference in behaviour in defining *global*
variables -- anything outside of all classes is actually available in
every class in the system, not just the classes in that module.
I'd consider adding module variables if we could come up with a good
way to do it, but I can't think of a mechanism.
In the meantime, though, scoped variables should get you pretty close:
#manifests/init.pp
class mymod {
$foo = "important stuff
}
#manifests/other.pp
class mymod::other {
$other = "even more ${foo}"
}
--
I take my children everywhere, but they always find their way
back home. --Robert Orben
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com
Isn't that missing an inherits or include of mymod in mymod::other ?
David
Derr, I mean "even more ${mymod::foo}".
--
The truth is that there is nothing noble in being superior to somebody
else. The only real nobility is in being superior to your former self.
-- Whitney Young