On Wednesday, December 11, 2013 10:02:00 AM UTC-6, Alexander Gray II wrote:
>
> I have a variable initialized within a define but I cannot reference it 
> from within a class.
>


Yes.

 

>
> contents of bar.pp:
> define foo::bar (
>   $my_var = 'this is my value',
> ){
>   notice("The value in define: ${my_var}")
>   include foo
> }
>


Once upon a time, Puppet had dynamic scoping of variables.  In that era 
(before Puppet 3), your code would have worked somewhat like you expected, 
though it is subtly buggy even so.  Dynamic scoping caused more problems 
than it solved, however, and removing it was one of the major changes 
between Puppet 2 and Puppet 3.

 

> contents of init.pp:
> class foo{
>   notice("The value in class: ${my_var}")
> }
>
> The problem is that the value of my_var within the class is empty.
>


Yes, because you never assign a value to it.  Unqualified variable names 
now always refer to either a variable in the local scope, if it exists, or 
else a variable in node scope, if it exists, or else a top-scope variable.

 

>
> For instance if do:
> class myclass(){
>   foo::bar{}
> }
> I see:
> The value in define: this is my value
> The value in class:
>
> How can a variable that is defined in a "define" be accessible from within 
> a class?
>
>

It can't.  Moreover, you have a conceptual problem here.  A Puppet 
definition defines a resource type.  More than one instance of that type 
may be declared, but classes are singletons.  Although classes may be 
declared multiple times (if you use 'include' or 'require') the second and 
subsequent declarations to be evaluated have no additional effect.  So, 
what would you be looking for Puppet to do if you told it

foo::bar { 'alice': my_var => 1 }
foo::bar { 'bob':   my_var => 42 }

?  Class variable $foo::my_var can have only one value, but which ought it 
to be? The answer doesn't actually matter -- the fact that the question 
even arises indicates a flaw in your design.

Perhaps you are about to claim that only one instance of foo::bar will ever 
be declared for any given node.  If that's the case, however, then why is 
it a definition in the first place, as opposed to being a class?


Your example too abstract for me to suggest alternatives.  If you describe 
the real-world problem you are trying to solve, then perhaps one of us will 
have ideas for you.


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 puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/1dded450-2db0-46d4-b176-a55ecad046a8%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to