check if a class exists

Michael Krupp picture Michael Krupp · Feb 26, 2013 · Viewed 17.3k times · Source

Is there a way to check in manifest files if a given class exists?

I want to do something like this:

class foo {
    if exists( Class["foo::${lsbdistcodename}"] ) {
        include foo::${lsbdistcodename}
    }
}

So I can easily add distrubution / version specific classes which are then automatically included.

Answer

LiorH picture LiorH · Feb 27, 2013

You should use defined instead of exists statement. The following snippet works for me:

class foo {
    if defined( "foo::${lsbdistcodename}") {
            notify {'defined':}             
            include "foo::${lsbdistcodename}"
    }
}

class foo::precise {
    notify{'precise':}
}

[assuming you're running puppet version > 2.6.0]