Tim Bishop
2012-Jul-20 20:40 UTC
[Puppet Users] Calling a definition multiple times but with different parameters
I''ve got a bit stuck trying to implement something in Puppet. I''m hoping I''m just looking at it the wrong way and somebody can point me in the right direction :-) So here''s what I have. A definition that creates a site (it doesn''t really matter what it''s creating): define mymodule::site ($path) { # ... do some stuff ... } mymodule::site { ''sitename'': path => ''/some/path'', } This is fine. But what I want to do is create an arbitrary set of symlinks along with the site. So I extend it something like this: define mymodule::site ($path, $links) { # ... do some stuff ... mymodule::links { $links: path => $path, } } define mymodule::links () { # create a link: $name -> $path } mymodule::site { ''sitename'': path => ''/some/path'', links => [ ''foo'', ''bar'' ], } So this works. It creates my site, and calls mymodule::links for each link separately. The problem comes if I want to call mymodule::links with more than just one changing parameter (I''m using $name above). I''d like to be able to do something like this, but can''t see how to make it work: mymodule::site { ''sitename'': path => ''/some/path'', links => [ { ''link1'' => ''target1'' }, { ''link2'' => ''target2'' }, ], } But you can''t pass a hash in through $name, and I can''t see how I can pass separate parameters each call of the definition. I''d like a for loop really! Any pointers? Am I looking at this completely the wrong way? The only alternative I''ve come up with is to just manually create the links with file resources where I call mymodule::site, but I ideally wanted to encapsulate all the behaviour within the module. Thanks in advance, Tim. -- Tim Bishop http://www.bishnet.net/tim/ PGP Key: 0x5AE7D984 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Mark Roggenkamp
2012-Jul-21 03:22 UTC
[Puppet Users] Re: Calling a definition multiple times but with different parameters
First, I''ll say this sort of thing comes up rather often. It seems that perhaps there''s a void in dynamic generation of resources based on some data structure (where you wish you had full langage features to do so). However, you may be able to make use of the create_resources function if it makes sense for you to have $links look like this: $links = { ''link1'' => { ''ensure'' => ''link'', ''target'' => ''target1'', }, } and then: create_resources(File, $links). the hash key will be used for $name passed to File and the value (hash) is used to feed the other params of File. You may also need to specify your own defined type that takes a base path instead of File. Or, you could use the prepend function in stdlib to pregenerate the file paths if need be. Or, regsubst. Good luck :) Mark On Friday, July 20, 2012 4:40:42 PM UTC-4, Tim Bishop wrote:> > I''ve got a bit stuck trying to implement something in Puppet. I''m hoping > I''m just looking at it the wrong way and somebody can point me in the > right direction :-) > > So here''s what I have. A definition that creates a site (it doesn''t > really matter what it''s creating): > > define mymodule::site ($path) { > # ... do some stuff ... > } > > mymodule::site { ''sitename'': > path => ''/some/path'', > } > > This is fine. But what I want to do is create an arbitrary set of > symlinks along with the site. So I extend it something like this: > > define mymodule::site ($path, $links) { > # ... do some stuff ... > mymodule::links { $links: > path => $path, > } > } > > define mymodule::links () { > # create a link: $name -> $path > } > > mymodule::site { ''sitename'': > path => ''/some/path'', > links => [ ''foo'', ''bar'' ], > } > > So this works. It creates my site, and calls mymodule::links for each link > separately. > > The problem comes if I want to call mymodule::links with more than just > one changing parameter (I''m using $name above). I''d like to be able to > do something like this, but can''t see how to make it work: > > mymodule::site { ''sitename'': > path => ''/some/path'', > links => [ { ''link1'' => ''target1'' }, { ''link2'' => ''target2'' }, ], > } > > But you can''t pass a hash in through $name, and I can''t see how I can > pass separate parameters each call of the definition. I''d like a for > loop really! > > Any pointers? Am I looking at this completely the wrong way? > > The only alternative I''ve come up with is to just manually create the > links with file resources where I call mymodule::site, but I ideally > wanted to encapsulate all the behaviour within the module. > > Thanks in advance, > > Tim. > > -- > Tim Bishop > http://www.bishnet.net/tim/ > PGP Key: 0x5AE7D984 >-- 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/-/ls16Czyh10cJ. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.