Check to see if a class is included

3,482 views
Skip to first unread message

Matt

unread,
Aug 20, 2009, 7:26:13 AM8/20/09
to puppet...@googlegroups.com
I looked at the language tutorial but couldn't see it anywhere.

Is there a way to check if a class is included i.e.

if class["myclass"] {
myvar = "hello"
}

Thanks,

Matt

James Turnbull

unread,
Aug 20, 2009, 8:33:50 AM8/20/09
to puppet...@googlegroups.com
Matt wrote:
> I looked at the language tutorial but couldn't see it anywhere.
>

You might be looking for the defined function.

http://reductivelabs.com/trac/puppet/wiki/FunctionReference#defined

Regards

James Turnbull

--
Author of:
* Pro Linux Systems Administration (http://tinyurl.com/linuxadmin)
* Pulling Strings with Puppet (http://tinyurl.com/pupbook)
* Pro Nagios 2.0 (http://tinyurl.com/pronagios)
* Hardening Linux (http://tinyurl.com/hardeninglinux)

signature.asc

Larry Ludwig

unread,
Aug 20, 2009, 9:23:36 AM8/20/09
to puppet...@googlegroups.com
Every class you create is a tag.

So:

if tagged('myclass') {
$myvar = "hello"
}

One caveat, is the class order can matter if the tag exists yet or not.

http://projects.reductivelabs.com/issues/2105

-L

--
Larry Ludwig
Reductive Labs

Matt

unread,
Aug 20, 2009, 9:34:18 AM8/20/09
to puppet...@googlegroups.com
I saw that but I think that did something else.

node "mynode" {
include myclass, foo
}

node "mynode2" {
include foo
}

class "foo" {
if defined(Class['myclass']) {myvar = "hello"})
}

Will both mynode2 and mynode get the variable myvar? In my case I
only want mynode too. If both do, then I guess I need to use tagged
as Larry said, but make sure that myclass is first in the include?

Thanks,

Matt

2009/8/20 James Turnbull <ja...@lovedthanlost.net>:

Ohad Levy

unread,
Aug 20, 2009, 10:50:33 AM8/20/09
to puppet...@googlegroups.com
if you have a complex variables setup, I really recommend to move away to external nodes(http://reductivelabs.com/trac/puppet/wiki/ExternalNodes) or extlookup(
http://nephilim.ml.org/~rip/puppet/extlookup.rb)

if you use external nodes, you could use an external source (e.g. a database, or web service like GNI) to define your variables on a node level.

my 0.01 cents
Ohad

Matt

unread,
Aug 20, 2009, 12:05:02 PM8/20/09
to puppet...@googlegroups.com
I want to keep away as much as possible from defining these variables
at the node level as they are more linked to the included classes.

I'm trying to set-up a monitoring module that looks at what
classes(modules) have been included for that node and then deploys the
required monitoring scripts.

Being able to build an array of filenames based on 'if class is
included' would do the trick quickly.

I guess another way would be for me to start building custom facters
of what a node is and then deploy the right files depending on the
facter result.

Anyone done similar? I always feel with puppet i'm missing something
obvious :-)

2009/8/20 Ohad Levy <ohad...@gmail.com>:

Ohad Levy

unread,
Aug 20, 2009, 12:10:27 PM8/20/09
to puppet...@googlegroups.com
On Fri, Aug 21, 2009 at 12:05 AM, Matt <mattm...@gmail.com> wrote:

I guess another way would be for me to start building custom facters
of what a node is and then deploy the right files depending on the
facter result.
 
Anyone done similar?  I always feel with puppet i'm missing something
obvious :-)

I think thats exactly what external nodes is design for...  

Peter Meier

unread,
Aug 20, 2009, 3:26:10 PM8/20/09
to puppet...@googlegroups.com
Hi

> I'm trying to set-up a monitoring module that looks at what
> classes(modules) have been included for that node and then deploys the
> required monitoring scripts.

For something like that I use exported resources, so within the class
itself I export a nagios-check, which then will be collected by the
nagios server and my service is monitored.

for example for something simple like a tcp port monitoring for silc:
http://git.puppet.immerda.ch/?p=module-silc.git;a=blob;f=manifests/init.pp;h=deecb092cbeaad7765a83c25c15bd939ab1a5f33;hb=d79d8c9d76599fb3dfdce2cf8d77dcd4c517bf97#l9

which includes the silc::nagios class, which declares a nagios::service
for silc:
http://git.puppet.immerda.ch/?p=module-silc.git;a=blob;f=manifests/nagios.pp;h=99ca7f970500882e45fe471670478a2444662823;hb=d79d8c9d76599fb3dfdce2cf8d77dcd4c517bf97

which is behind (in the nagios module) an exported nagios resource to
the nagios server.

a bit an alternative approach, but might fit your needs, doesn't it?

cheers pete

Matt

unread,
Aug 21, 2009, 6:19:23 AM8/21/09
to puppet...@googlegroups.com
Peter,

What sets:

if $use_nagios {
include silc::nagios
}


Thanks,

Matt

2009/8/20 Peter Meier <peter...@immerda.ch>:

Peter Meier

unread,
Aug 21, 2009, 6:22:45 AM8/21/09
to puppet...@googlegroups.com
Hi

> What sets:
>
> if $use_nagios {
> include silc::nagios
> }
>


well I build my modules, so you can use them also without nagios
(default) so if you'd like to use nagios you set globally in your
site.pp the variable $use_nagios = true

It's just a variable which enable certain more features on the modules.

cheers pete

Matt

unread,
Aug 21, 2009, 6:28:09 AM8/21/09
to puppet...@googlegroups.com
Looks sensible to me. Thanks Peter i'll give that way a go.

2009/8/21 Peter Meier <peter...@immerda.ch>:
Reply all
Reply to author
Forward
0 new messages