Steve Shipway
2010-Nov-03 21:43 UTC
[Puppet Users] How to use a resource name in its own definition?
Hi, I''m trying to set up Puppet here for the first time and have run into some problems. To set up our users, I''m trying to define many users at once as virtual resources for later realisation in sets. However, I''d like to have it properly create the home directory: @user { ''foo'': ensure => "present", managehome => true, home => $operatingsystem ? { solaris => "/export/home/foo", default => "/home/foo", }, } Now, this works; what I want to do is to make the ''foo'' bit parameterised, so that I can do: $admins = [ ''foo'', ''bar'' ] @user { $admins: ensure => "present", managehome => true, home => $operatingsystem ? { solaris => "/export/home/$name", default => "/home/$name", }, } However this doesn''t work; the $name is taken from the class that we''re running this in. Is there any way to do this? I''d use a loop over the $admins list, except that I cant find any loop structures in the puppet config language, only if/else. I''ve tried using a define to do it: define users::addVirt() { @user { $name: ensure => "present", managehome => true, home => $operatingsystem ? { solaris => "/export/home/$name", default => "/home/$name", }, } } class users { $admins = [ ''foo'', ''bar'' ] users::addVirt { [ $admins ]: } } and this works; however when I later try to realise the users: class users::admins inherits users { $admingroup = $operatingsystem ? { solaris => ''sysadmin'', default => ''unixadmin'' } User[ $admins ] { groups +> [ $admingroup ], } realize( User[ $admins ] ) } I end up with: err: Could not retrieve catalog from remote server: Error 400 on SERVER: Only subclasses can override parameters at /etc/puppet/manifests/classes/users.pp:96 on node secpupprd01.its.auckland.ac.nz which is complaining about the part: User[ $admins ] { groups +> [ $admingroup ], } although I cannot see why. Does anyone have any advice for me on what is going wrong here, or a better way to achieve this? Thanks in advance for any help... Steve Steve Shipway University of Auckland ITS UNIX Systems Design Lead s.shipway@auckland.ac.nz Ph: +64 9 373 7599 ext 86487 -- 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.
jcbollinger
2010-Nov-03 22:30 UTC
[Puppet Users] Re: How to use a resource name in its own definition?
On Nov 3, 4:43 pm, "Steve Shipway" <s.ship...@auckland.ac.nz> wrote:> To set up our users, I''m trying to define many users at once as virtual > resources for later realisation in sets. However, I''d like to have it > properly create the home directory[... and]> I want to [...] make the [username] parameterised[...]> I''ve tried using a define to do it: > > define users::addVirt() { > @user { $name: > ensure => "present", > managehome => true, > home => $operatingsystem ? { > solaris => "/export/home/$name", > default => "/home/$name", > }, > }} > > class users { > $admins = [ ''foo'', ''bar'' ] > users::addVirt { [ $admins ]: } > > } > > and this works;As it should. A define wrapping the resource declaration is just what you need to use for this purpose.> however when I later try to realise the users: > > class users::admins inherits users { > $admingroup = $operatingsystem ? { > solaris => ''sysadmin'', > default => ''unixadmin'' > } > User[ $admins ] { > groups +> [ $admingroup ], > } > realize( User[ $admins ] ) > > } > > I end up with: > > err: Could not retrieve catalog from remote server: Error 400 on SERVER: > Only subclasses can override parameters at > /etc/puppet/manifests/classes/users.pp:96 on node > secpupprd01.its.auckland.ac.nz > > which is complaining about the part: > > User[ $admins ] { > groups +> [ $admingroup ], > } > > although I cannot see why.A Puppet define is not a macro. Roughly speaking, your User resources do not belong to class users, but rather to instances of your define, users::addVirt, which is why a subclass of users cannot override their properties. You could try nesting the define inside class users, but I don''t really think that''s gonna work (it would be sweet if it did, though!). More likely, you will either need to enumerate all the users or to modify your define so that it assigns the needed group(s) at the place the users are initially declared. Alternatively, you might be able to use parameterized classes (new in 2.6.x) instead of defines to do this job, but I''m not sure offhand how nicely they play with inheritance or array parameters. Good Luck, John -- 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.
Steve Shipway
2010-Nov-04 01:28 UTC
RE: [Puppet Users] Re: How to use a resource name in its own definition?
> A Puppet define is not a macro. Roughly speaking, your User resources > do not belong to class users, but rather to instances of your define, > users::addVirt, which is why a subclass of users cannot override their > properties. You could try nesting the define inside class users, but > I don''t really think that''s gonna work (it would be sweet if it did, > though!).This explains a lot, then. I was thinking of a define as a macro or a function block, whereas in fact it is more like a class (albeit with multiple instances). A parameterised class isn''t going to work. Maybe I can make users::virt as the define, and then then a subclass users::virt::admins which inherits users and is included by users::admins in order to get around this? I''ll try it and see what it says... Thanks for the help; in a couple of days the Puppet book should arrive here and hopefully fill in the large gaps in the online manual for us. Steve Steve Shipway steve@steveshipway.org Routers2.cgi web frontend for MRTG/RRD; NagEventLog Nagios agent for Windows Event Log monitoring; check_vmware plugin for VMWare monitoring in Nagios and MRTG; and other Open Source projects. Web: http://www.steveshipway.org/software Please consider the environment before printing this e-mail -- 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.