|
Have to think about this as it is like calling an external function and that function has access to all your local variables. That is truly horrible and it leads to problems like not being able to reuse templates. It is unspecific, not robust, not type safe, impure, (I can go on about this). It is ok for inline epp as it works like a lambda, it is embedded in the context it gets variables from. With external EPP from a file it is like an external function.
I do recognize the pain and I would like to help making it easier as you now manually have to assemble all variables and give them to the template - or access them via fully qualified names.
Note that there is new syntax that makes it easy to import a bunch of names at the same time from another namespace.
[$param1, $param2, $param3] = Class[foo::bar::fee::fum]
|
Would give you local variables $param1... etc. from $foo::bar::fee::fum::param1, $foo::bar::fee::fum::param2, etc. Which saves a lot of typing.
Ideally, a template should not pull anything at all from anywhere and only rely on what is given to it, and its parameters should be declared and typed. Then instead of empty strings, wrong data type, etc. will be caught automatically instead of just the "wrong text" being produced.
Currently you have to supply arguments as a hash. Maybe we can do something about that so that becomes easy(er/ish).
You can always do this:
class foo($a= 10, $b=20) {
|
}
|
include foo
|
notice inline_epp(@(EPP), {context => Class[foo]})
|
<% |Type[Class[foo]] $context | %>
|
$a = <%= $context[a] %>
|
$b = <%= $context[b] %>
|
EPP
|
This uses an inline EPP, but done such a way that it works for an external file as well.
-
The EPP defines a parameter $context, and it types it so it only accepts Class[foo]
-
It then uses $context[param] to get the parameters from the class (this works for resources as well)
-
When calling the template, a reference is sent to the class.
This works if:
-
only accessing the class/resource parameters and not any other variables.
To also mix in variables, add them to the hash in a fitting maner.
IMO, the "multi assign" works best - here is an example that shows that in an EPP
class foo($a= 10, $b=20) {
|
$var = 100
|
}
|
include foo
|
notice inline_epp(@(EPP), {context => Class[foo]})
|
<% |Type[Class[foo]] $context | -%>
|
<% [$a, $b, $var] = $context %>
|
$a = <%= $a %>
|
$b = <%= $b %>
|
$var = <%= $var %>
|
EPP
|
That I think is the current best (and safe) approach to getting the context into an EPP template. Now, if you have two "compatible contexts", you can relax the typing of the parameter, or go full "give me anything and if you don't have the variables I will error" mode.
The 'multi assign from class" works from Puppet 4.5.0. If you are calling from within a user defined resource, there is no way to get to its local variables from the outside, and you have to pass them as arguments (the parameters of a resource can be accessed though).
I hope this adds missing pieces to the puzzle on how it is intended to use the EPP implementation.
|