if defined not working when class is enlcosed within Class[ ]

307 views
Skip to first unread message

Florian Gray Jones

unread,
Aug 19, 2013, 11:33:09 AM8/19/13
to puppet...@googlegroups.com
Hi,

I am learning puppet using version 2.7.18 on a fresh ubuntu install.

I am trying to test this module https://github.com/liamjbennett/puppet-sabnzbd/blob/master/manifests/init.pp but I'm having trouble with the if defined(Class['supervisor::service']) section in that it doesn't get run even though the supervisor module is installed.

If i put "else{ notice("supervisor not defined?") }" on the end then I see that notice msg in the 'puppet apply --debug' output.

This
supervisor module is downloaded/installed OK from Puppet Forge and if I either

a) comment out the 'if defined' then it works as expected
b)
change the if defined(Class['supervisor::service']) to read if defined('supervisor::service') then it also works as expected.

As you can see there is an "include supervisor" at top of the class which I'm guessing isn't being called in the
order needed for the 'if defined' to work and the one without the
Class[ ] must work because puppet auto loads the modules from the modulepath
and checks if that namespace and module exist?

This is all new to me so i could be very wrong but can anyone explain given that init.pp why the 'if defined' isn't working and what is the best
way to achieve what the 'if defined' is there for and that's to see if the supervisor module is installed/loaded before entering the if statement and using the module?

Many thanks for reading
fLo



Ellison Marks

unread,
Aug 19, 2013, 2:24:29 PM8/19/13
to puppet...@googlegroups.com
So, I can't speak to the authors intentions, but I'll do my best to explain the behavior.

As you guessed defined() behaves differently based on what is passed to it. If passed a resource reference, as it is in the module, it will check that that resource has been declared. if passed a string, it will treat it as a type/class name and make sure that the resource has been defined. So, as written, it's checking if a class by the name of supervisor::service has been declared, while the way you write it, it check if anything by the name of supervisor::service has been defined.

The 'include supervisor' you noticed does nothing regarding this error, as the type in question is supervisor::service.

If I had to guess, at one point in the past, supervisor::service was a class, while it is now a defined type, and the module you're working with hasn't updated or something.

Florian Gray Jones

unread,
Aug 19, 2013, 11:32:39 PM8/19/13
to puppet...@googlegroups.com
ok, that's great. Thanks for the help.

The puppet-docs it says: "Modules are how Puppet finds the classes and types it can use — it automatically loads any class or defined type stored in its modules."

With this in mind, if "
if defined(Class[ ])" checks for classes.. and in my case 'supervisor::service' is as you mentioned not a class but a define. Is there an equivalent like "if defined(Define[ ])" or is best practice to continue using "if defined('supervisor::service')" to just check for anything of that name.

Thanks again
fLo

Ellison Marks

unread,
Aug 20, 2013, 12:38:29 AM8/20/13
to puppet...@googlegroups.com
Since in this case, it's checking that supervisor::service exists so it can declare one, I'd say checking for just 'supervisor::service' would be fine. You may want to stay around and see what other people think though, as I could be missing something.

Looking at it again, the check to see if a class has been declared seems really odd... and testing it now that I have a second, it doesn't actually seem to work at all. I'm not sure how idiomatic that is, or was, but maybe someone else can shed some light on it.

jcbollinger

unread,
Aug 20, 2013, 9:40:10 AM8/20/13
to puppet...@googlegroups.com


On Monday, August 19, 2013 10:32:39 PM UTC-5, Florian Gray Jones wrote:
ok, that's great. Thanks for the help.

The puppet-docs it says: "Modules are how Puppet finds the classes and types it can use — it automatically loads any class or defined type stored in its modules."



Where did you see that?  Although true, it may be a bit misleading to new Puppeteers, as "load" in this context does not mean the same thing as "declare".  That is, the classes in installed modules are not automatically assigned to any node; you must still declare them for the nodes where you want them.

 
With this in mind, if "if defined(Class[ ])" checks for classes.. and in my case 'supervisor::service' is as you mentioned not a class but a define. Is there an equivalent like "if defined(Define[ ])" or is best practice to continue using "if defined('supervisor::service')" to just check for anything of that name.


The expression "defined(Class[<classname>])" checks for the named class being already declared for the target node.  The expression "defined(<classname>)" on the other hand, checks whether a class or resource type is known to Puppet, so that it is available to be declared for the target node.  The form you suggest, "defined(Define[<title>])" does not make sense, but the either the condition  "defined(Supervisor::Service['sabnzbd'])" or the condition "defined 'supervisor::service'" should work, depending on which you mean (they are not equivalent).

Now, having said all that, I leave you with one more piece of advice: don't do it.  The usual use of the defined() function, wherein its argument is a resource reference, introduce a parse-order dependency into your manifest set, and that's bad news for you.  The other form, where you test for a class or resource type being available, is less problematic, but usually not what you want.  It is rarely the case that the desired response to a resource type or class being undefined is anything other than catalog compilation failure, and you don't need defined() for that.


John

Reply all
Reply to author
Forward
0 new messages