On Wednesday, April 30, 2014 1:31:03 PM UTC-5, drs wrote:
>
> I am new to puppet and am trying to figure out variable scoping. I have a
> test module that I am using for this and it looks like this:
>
> manifiests/init.pp:
>
> class test {
> include test::params
>
> file { 'testfile' :
> path => "/tmp/testfile",
> content => template("test/testfile.erb"),
> }
> }
>
> manifests/params.pp:
>
> class test::params {
> $p1 = hiera('param1')
> $p2 = hiera('param2')
> }
>
> templates/testfile.erb:
>
> this is the first parameter: <%= @p1 %>
> this is the second parameter: <%= @p2 %>
>
> global.yaml:
>
> ---
> param1: "this is the first one"
> param2: "this is the second one"
>
> Based on what I am seeing in the ProPuppet book (the discussion of the
> puppet module in Chap. 2, pp69-70), this should work. However, the values
> are not being inserted in the template. In order to get them in, I have to
> add something like this to the init.pp manifest:
>
> $a = $::test::params::p1
> $b = $::test::params::p2
>
> and reference @a and @b in the template.
>
> So the question is: Am I missing something or is there an error in the
> ProPuppet example?
>
I am not looking at *Pro Puppet*, but probably you are missing something:
templates can refer directly to only global variables and the local
variables of the class where the template is evaluated. When one class
declares another, such as via 'include', the other class's variables do not
become local to the including class (see also below). For a template to
reference variables of some other class than the one currently evaluating
it, it must use scope.lookupvar('qualified::name::of::desired::variable').
Note 1: Although declaring another class does not make its variables local,
inheriting from another class does effectively do so. But use class
inheritance for that purpose.
Note 2: Do not misunderstand the 'include' function. It "includes" the
named class in the target node's catalog. It does *not* include or
interpolate the class or any part of it into the class where the 'include'
statement appears.
>
> If I need to add the $a = …. and $b=… lines to the init.pp, what, if any
> is the advantage to having a params.pp manifest. I could just put the
> hiera() calls in init.pp.
>
>
The common understanding of the purpose of ::params class is to serve as a
data repository, typically of hard-coded, module-specific data. Such data
most often serve as default values for class variables, including class
parameters, and sometimes serve as general well-known values of the
module's domain. (To safely use a ::params class for parameter defaults,
the parameterized class must inherit from the ::params class.) There has
also been a growing tendency to put default data computations into such a
class, though there is some doubt about the wisdom of that approach.
And you can indeed just put hiera() calls into one of your module's other
classes instead, or allow puppet to make such calls automatically to select
class parameter values. For data ultimately coming from hiera, that's
precisely what I would do.
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/4a5b685a-fd46-46fb-bfba-1cc3cf227463%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.