As you might already know, I like pushing the boundaries of what puppet is able to do. Today, I realized that I needed to include a class by variable name. A simple way to do this is possible with:
$foo = 'world'
class { "hello::${foo}::params":
}
I then realized that you could also do this with the standard include keyword:
$foo = 'world'
include "hello::${foo}::params"
If you’re really insane enough to need to have your include depend on a variable, then this should suit your needs. The advantage of the second form is that if that statement appears more than once, you won’t cause a “duplicate definition” error.
Recently, I’ve been playing around with hiera. For completeness, I will mention one more form. It turns out that the hiera puppet functions offer a hiera_include function. As with all the hiera functions, the second argument lets you specify a default:
$foo = 'world'
hiera_include('...', "hello::${foo}::params")
which finishes off the trilogy. Hope this was useful, now start getting creative and
Happy hacking,
James
Thanks for this post.
I used defined():
if defined( “foo::${bar}”) {
include “foo::${bar}”
}
works like a charm. :)
Actually, I don’t think you should do this… the ‘defined’ function (as I remember it) was an old hack from the early puppet days… I think it can be “considered harmful” because I thought it had some issue with ordering and how it plays with the graph…
In any case, if you do want that sort of functionality, I think you want ensure_resource instead.