New Scope > 2.7

46 views
Skip to first unread message

Douglas Garstang

unread,
Aug 10, 2012, 4:28:33 PM8/10/12
to Puppet Users
So...

I was just reading the new puppet scoping documentation at
http://docs.puppetlabs.com/guides/scope_and_puppet.html.

I don't get it. If I have this...

class web_server {
include common
include webserver
$my_role = "web_server"
}

Can I access the $my_role variable in the webserver class?

Doug.

llowder

unread,
Aug 10, 2012, 4:42:43 PM8/10/12
to puppet...@googlegroups.com
 
If you use: $web_server::my_role
 
Doug.

Douglas Garstang

unread,
Aug 10, 2012, 4:52:42 PM8/10/12
to puppet...@googlegroups.com
This seems completely screwed to me. What if your in a general class,
one not necessarily related to the function of a web server (but still
included from a web server), and you need to access the role?

Doug.

llowder

unread,
Aug 10, 2012, 5:24:00 PM8/10/12
to puppet...@googlegroups.com

Then use the fully qualified variable name as I mentioned in my last post.

 
Doug.

Douglas Garstang

unread,
Aug 10, 2012, 5:27:58 PM8/10/12
to puppet...@googlegroups.com
What if the class I am in doesn't KNOW that the parent is $web_server ...?

Doug.

Denmat

unread,
Aug 10, 2012, 6:11:37 PM8/10/12
to puppet...@googlegroups.com
What if you've declared my_role in several classes? This provides certainty. Alternatively you could make a fact that has the value you want of my_role and reference that in the top scope $::my_role.

Dan White

unread,
Aug 10, 2012, 7:19:38 PM8/10/12
to puppet...@googlegroups.com
It is called: "Making an honest attempt to help you avoid shooting your own toes off".

By forcing you to specifically reference the class name along with the variable name (that includes the top scope $::foo), it saves you extensive grief down the road when you eventually end up with the same variable name in multiple classes.

Or would you rather have to track all the variable names in all the classes you use (the ones you write yourself along with ones you pull from places like the Puppet Forge) to ensure unique variable names ?


Mark Roggenkamp

unread,
Aug 11, 2012, 10:24:56 AM8/11/12
to puppet...@googlegroups.com
You may want to consider making whatever needs $my_role into a define or class that can be used in another module. Then make a module to contain roles. These roles would be classes (since puppet has no role resources type like this) that include relevant defines/classes from other modules. I've found for my use cases composing things that way works a bit better, though of course YMMV.

Regards,
Mark

James Sweeny

unread,
Aug 11, 2012, 10:39:28 AM8/11/12
to puppet...@googlegroups.com
Parameterized classes were also made for this issue, but I don't recommend going that route if you have several levels of classes being included before you have to use the variable.

You might find it worthwhile to set $my_role in Hiera, and provide a sane default within the webserver class.

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/_6R6QOKDXugJ.

To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.



--

James Sweeny

Professional Services


Reply all
Reply to author
Forward
0 new messages