Wrap class around define and use it as Require

50 views
Skip to first unread message

yamaka...@gmail.com

unread,
Jul 30, 2014, 8:30:20 PM7/30/14
to puppet...@googlegroups.com
Hi Guys,

I'm using an ENC to define all my nodes including some modules I want to load, I have the following class/define for this:

class mymodule::modules::enable (
   
    $enable_modules = undef
) {

     $modules_split = split($enable_modules, ',')
     enableModules { $modules_split: }
 
}

define enableModules () {
     exec { "mycommand  $name" :
          command   => "/usr/sbin/mycommand $name",
          notify => Service[myservice]
     }
}

mymodule::modules::enable{'modX': enable_modules => 'module'}


Now I'm using this per node using mod1,mod2,mod3 which works OK, I don't want to use create_resources here as I want to have one filed for enable, and one field for disable in my ENC

For some fool-proof solution that when the module is not set in my ENC I want to require the define enableModules in a different class and set a static parameter:

some::class ( )

{

Class['modulename'] -> MyModule::Modules::Enable['modX:']

file { "/my/path/to/directory" :
                                ensure  => directory,
                                mode    => '0644',
                                owner   => 'root',
                                group   => 'root',
                                require => Class['modulename'],
                              }

}

Well, I need to wrap a class around my define to get this all working as you can see above but this is not working as my some::class says:

Could not find dependency Class[Enable_mymodule] for File[/my/path/to/directory] at...

I have tried includes, different ways of defining or wrapping the class but no clue at all, I also get other errors that are not related as I was messing things more up.

When I use:

Class['modulename] -> class { 'mymodule::modules::enable': enable_modules => 'mymodule' }

I get the message that it cannot redeclare 'mymodule::modules::enable' as my ENC already did.


I have used some Foo parameters and Vars here as I want to use this in more modules if this is possible. It's just for a fallback when modules are needed for sofware to be loaded and the user didn't enable it in it's ENC yaml.

I hope someone can help me out.

Thanks,

Matt

jcbollinger

unread,
Jul 31, 2014, 9:44:57 AM7/31/14
to puppet...@googlegroups.com


On Wednesday, July 30, 2014 7:30:20 PM UTC-5, yamaka...@gmail.com wrote:
Hi Guys,

I'm using an ENC to define all my nodes including some modules I want to load, I have the following class/define for this:

class mymodule::modules::enable (
   
    $enable_modules = undef
) {

     $modules_split = split($enable_modules, ',')
     enableModules { $modules_split: }
 
}

define enableModules () {
     exec { "mycommand  $name" :
          command   => "/usr/sbin/mycommand $name",
          notify => Service[myservice]
     }
}


You should give that definition a properly-qualified and preferrably all-lowercase name (maybe mymodule::modules::enable_modules) and put it in a corresponding file (<modulepath>/mymodule/manifests/modules/enable_modules.pp).  You should everywhere refer to it via its fully-qualified name.

 

mymodule::modules::enable{'modX': enable_modules => 'module'}



That must not appear at the top level of any manifest other than a starting-point manifest.  In particular, if you follow the correct module layout then it must not appear at to scope in any manifest file in your module.  Put it in a class, in a node block if you are using those, or in a starting-point manifest.

 

Now I'm using this per node using mod1,mod2,mod3 which works OK, I don't want to use create_resources here as I want to have one filed for enable, and one field for disable in my ENC

For some fool-proof solution that when the module is not set in my ENC I want to require the define enableModules in a different class and set a static parameter:

some::class ( )

{

Class['modulename'] -> MyModule::Modules::Enable['modX:']

file { "/my/path/to/directory" :
                                ensure  => directory,
                                mode    => '0644',
                                owner   => 'root',
                                group   => 'root',
                                require => Class['modulename'],
                              }

}

Well, I need to wrap a class around my define to get this all working as you can see above but this is not working as my some::class says:

Could not find dependency Class[Enable_mymodule] for File[/my/path/to/directory] at...


Class[enable_mymodule] does not exist in the code you presented.  Do you perhaps mean to write (assuming the renaming described above)

  require => Mymodule::Modules::Enable_modules['modulename']

?

Otherwise, I can't advise you very well about code I haven't seen.  I can say that this whole thing is looking pretty convoluted.  I don't have time at the moment to analyze it well enough to suggest a better alternative, but I'll try to come back to it later.


John

jcbollinger

unread,
Jul 31, 2014, 4:24:51 PM7/31/14
to puppet...@googlegroups.com


On Wednesday, July 30, 2014 7:30:20 PM UTC-5, yamaka...@gmail.com wrote:

mymodule::modules::enable{'modX': enable_modules => 'module'}




In fact, not only must that not appear at top level (outside a starting-point manifest), it must not appear at all. mymodule::modules::enable is a class not a resource type.


John

jcbollinger

unread,
Jul 31, 2014, 4:50:34 PM7/31/14
to puppet...@googlegroups.com

I came back to look at this, and I'm really not following what you are trying to do for fall-back behavior.  I suspect you have some misapprehensions that contribute both to your difficulty finding a solution to your problem, and to your difficulty explaining what you are trying to do.  Rather than guess at what you want to do, I offer some of the issues about which I think you might be confused:

1. Neither defined types nor defined type instances are classes.  The latter are resources.  Defined types themselves are, as their name implies, resource types, analogous to Puppet's built-in types such as File and Service.

2. The syntax for a reference to a class, such as you use with the chain operators or the 'require' metaparameter is Class['my::class::name'].  A reference of the form Mymodule::Some::Type['foo'] refers to a resource whose title is 'foo' and whose type is Mymodule::Some::Type (which is presumably a defined type if it has a qualified name like that).

3. The chaining arrows and the 'require' and 'before' metaparameters are exclusively about the relative order in which resources are applied by the agent.  They must refer to resources and/or classes that are ultimately in the catalog, but they themselves do nothing whatever to put those resources in the catalog.

4. The 'include', 'require', and 'contain' functions (not to be confused with resource metaparameters) cause classes to be included in the catalog.  The 'require' and 'contain' functions additionally have effects on the order in which the agent applies classes.  These can be used on classes that are already in the catalog, which in that case has only the application-order consequences (if any) that the functions carry.


John

yamaka...@gmail.com

unread,
Jul 31, 2014, 4:51:37 PM7/31/14
to puppet...@googlegroups.com
Hi John,

Thanks so far!


This is what I use now:

class mypuppetmodule::modules::enable (
       
        $enable_modules = undef
) {
 
 
     $modules_split = split($enable_modules, ',')
     enableModules { $modules_split: }
     
     enableModules { 'mod1':}
 
}

The issue is that when I do a require => Class['mypuppetmodule::modules::enable::ssl'] it says it cannot find the dependency.

I think this is the right way and include mypuppetmodule::modules::enable doesn't help me also.

Cheers,

Matt




Op donderdag 31 juli 2014 22:24:51 UTC+2 schreef jcbollinger:

jcbollinger

unread,
Jul 31, 2014, 5:30:16 PM7/31/14
to puppet...@googlegroups.com


On Thursday, July 31, 2014 3:51:37 PM UTC-5, yamaka...@gmail.com wrote:
Hi John,

Thanks so far!


This is what I use now:

class mypuppetmodule::modules::enable (
       
        $enable_modules = undef
) {
 
 
     $modules_split = split($enable_modules, ',')
     enableModules { $modules_split: }
     
     enableModules { 'mod1':}
 
}

The issue is that when I do a require => Class['mypuppetmodule::modules::enable::ssl'] it says it cannot find the dependency.



You have not presented any class named mypuppetmodule::modules::enable::ssl.  If you in fact have such a class then Puppet would expect to find its definition in manifest file <modulepath>/mypuppetmodule/manifests/modules/enable/ssl.pp.  Moreover, in order to require => that class you must ensure that it is declared at some point during catalog compilation.  The 'require' metaparameter does not do that for you (see my point (3)).

HOWEVER, I suspect that you don't actually have such a class at all, and you are instead trying to refer to some kind of artifact of 'ssl' being among the substrings specified to class mypuppetmodule::modules::enable via its parameter $enable_modules.  But Puppet has no such concept.  You could consider using one of these instead:

  require => Class['mypuppetmodule::modules::enable']

or

  require => EnableModules['ssl']

Even so, it remains unclear to me what you hope to gain by that.  Perhaps it's exactly what you need, but I can't tell.


John

yamaka...@gmail.com

unread,
Jul 31, 2014, 5:39:08 PM7/31/14
to puppet...@googlegroups.com
HI John,

Thanks!

I had that class in my enable.pp and not in modules/enable/ssl.pp

Is it not possible at all to place it in modules/enable.pp ?

Thanks a lot for you great explanation... just didn't noticed it as I try to create a less pp's of possible.

Cheers,

Matt

Op donderdag 31 juli 2014 23:30:16 UTC+2 schreef jcbollinger:

jcbollinger

unread,
Aug 1, 2014, 9:17:04 AM8/1/14
to puppet...@googlegroups.com


On Thursday, July 31, 2014 4:39:08 PM UTC-5, yamaka...@gmail.com wrote:
HI John,

Thanks!

I had that class in my enable.pp and not in modules/enable/ssl.pp

Is it not possible at all to place it in modules/enable.pp ?


It is possible to put the class there, but you should not do so because you risk breakage.  Very likely some things would work, but others would break.  I strongly recommend that you follow the standard module layout.
 

Thanks a lot for you great explanation... just didn't noticed it as I try to create a less pp's of possible.


I fail to see the point of doing so.  Not only does the standard module layout make it easier for Puppet to find your classes and defined types, it also makes that easier for you.  The perceived advantage of putting different classes and/or definitions in the same file is lost on me.


John

Reply all
Reply to author
Forward
0 new messages