Martin Langhoff
2013-Jun-10 20:41 UTC
[Puppet Users] any elegant way to iterate/map over data types?
Sysadmins have the (reasonable?) expectation of installing more than one ssh key. Relevant bits from my current config follows: class rl_users { define ssh_user($uid, $gid, $password, $akey, $ensure=present) { user{ $name : ensure => $ensure, managehome => true, uid => $uid, gid => $gid, password => $password, groups => [''wheel''], require => Group[$name], } group { $name : ensure => $ensure, gid => $gid, } ssh_authorized_key { "${name}-akey": ensure => $ensure, key => $akey, type => ''ssh-rsa'', user => $name, require => User[$name], } } @ssh_user { ''foo'': uid=> 2004 , gid => 2004, password => ''$6$foo'', akey => ''AAAAB3xyz/VFwxhtYhw=='', } # how can we support user bar? @ssh_user { ''bar'': uid=> 2005 , gid => 2005, password => ''$6$bar'', akey => [ ''AAAAB3xyz/VFwxhtYhw=='', ''''AAAABzzzzz=='' ] } Right now I have a fugly kludge in place to support a second "akey0" slot. m -- martin.langhoff@gmail.com - ask interesting questions - don''t get distracted with shiny stuff - working code first ~ http://docs.moodle.org/en/User:Martin_Langhoff -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Rilindo Foster
2013-Jun-10 22:08 UTC
Re: [Puppet Users] any elegant way to iterate/map over data types?
Starting in Pupet 3.2, there is experimental support for iteration: http://docs.puppetlabs.com/puppet/3/reference/lang_experimental_3_2.html#collection-manipulation-and-iteration On Jun 10, 2013, at 3:41 PM, Martin Langhoff <martin.langhoff@gmail.com> wrote:> Sysadmins have the (reasonable?) expectation of installing more than > one ssh key. > > Relevant bits from my current config follows: > > class rl_users { > define ssh_user($uid, $gid, $password, $akey, $ensure=present) { > user{ $name : > ensure => $ensure, managehome => true, > uid => $uid, gid => $gid, > password => $password, > groups => [''wheel''], > require => Group[$name], > } > group { $name : > ensure => $ensure, > gid => $gid, > } > ssh_authorized_key { "${name}-akey": > ensure => $ensure, > key => $akey, > type => ''ssh-rsa'', > user => $name, > require => User[$name], > } > } > > @ssh_user { ''foo'': > uid=> 2004 , gid => 2004, > password => ''$6$foo'', > akey => ''AAAAB3xyz/VFwxhtYhw=='', > } > > # how can we support user bar? > @ssh_user { ''bar'': > uid=> 2005 , gid => 2005, > password => ''$6$bar'', > akey => [ ''AAAAB3xyz/VFwxhtYhw=='', > ''''AAAABzzzzz=='' ] > } > > Right now I have a fugly kludge in place to support a second "akey0" slot. > > > > m > -- > martin.langhoff@gmail.com > - ask interesting questions > - don''t get distracted with shiny stuff - working code first > ~ http://docs.moodle.org/en/User:Martin_Langhoff > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. > To post to this group, send email to puppet-users@googlegroups.com. > Visit this group at http://groups.google.com/group/puppet-users?hl=en. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Matthias Saou
2013-Jun-11 11:36 UTC
Re: [Puppet Users] any elegant way to iterate/map over data types?
On Mon, 10 Jun 2013 16:41:22 -0400 Martin Langhoff <martin.langhoff@gmail.com> wrote:> Sysadmins have the (reasonable?) expectation of installing more than > one ssh key. > > Relevant bits from my current config follows: > > class rl_users { > define ssh_user($uid, $gid, $password, $akey, > $ensure=present) { user{ $name : > ensure => $ensure, managehome => true, > uid => $uid, gid => $gid, > password => $password, > groups => [''wheel''], > require => Group[$name], > } > group { $name : > ensure => $ensure, > gid => $gid, > } > ssh_authorized_key { "${name}-akey": > ensure => $ensure, > key => $akey, > type => ''ssh-rsa'', > user => $name, > require => User[$name], > } > } > > @ssh_user { ''foo'': > uid=> 2004 , gid => 2004, > password => ''$6$foo'', > akey => ''AAAAB3xyz/VFwxhtYhw=='', > } > > # how can we support user bar? > @ssh_user { ''bar'': > uid=> 2005 , gid => 2005, > password => ''$6$bar'', > akey => [ ''AAAAB3xyz/VFwxhtYhw=='', > ''''AAAABzzzzz=='' ] > } > > Right now I have a fugly kludge in place to support a second "akey0" > slot.One workaround which comes to mind is to use regsubst on the $akey array in order to make each element unique, and move the ssh_authorized_key call to its own definition. When it comes to iterating with puppet, the usual way to get where you want is to apply a definition to an array. From there, you need to avoid the (also usual) duplicate declarations, by extending and abusing the $title if needed in order to make sure it''s unique. So here (these are quick hints, completely untested), something like this should work, since "<user>-<sshkey>" is unique : $user-akey = regsubst($akey, ''^(.*)$'', "${name}-\1") my_ssh_authorized_key { $user-akey: ensure => $ensure } Then : define my_ssh_authorized_key ( $ensure ) { $user = regsubst($title, ''^(.+)-(.+)$'', ''\1'') $akey = regsubst($title, ''^(.+)-(.+)$'', ''\2'') ssh_authorized_key { $title: ensure => $ensure, key => $akey, type => ''ssh-rsa'', user => $user, require => User[$user], } } If there are more elegant solutions, I''d love to hear about them :-) Matthias -- Matthias Saou ██ ██ ██ ██ Web: http://matthias.saou.eu/ ██████████████ Mail/XMPP: matthias@saou.eu ████ ██████ ████ ██████████████████████ GPG: 4096R/E755CC63 ██ ██████████████ ██ 8D91 7E2E F048 9C9C 46AF ██ ██ ██ ██ 21A9 7A51 7B82 E755 CC63 ████ ████ -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Martin Langhoff
2013-Jun-11 22:55 UTC
Re: [Puppet Users] any elegant way to iterate/map over data types?
On Tue, Jun 11, 2013 at 7:36 AM, Matthias Saou <matthias@saou.eu> wrote:> When it comes to iterating with puppet, the usual way to get where you > want is to apply a definition to an array. From there, you need to avoid > the (also usual) duplicate declarations, by extending and abusing the > $title if needed in order to make sure it''s unique.This graf gave me what I needed, thanks! I used md5 to give each key a unique name and avoid the key appearing twice in the file, which makes the file unreadable. class rl_users { ## "ssh_user" pulls together the handling of ## - usergroup ## - user ## - ssh key ## which normally Puppet tracks independently define ssh_user($uid, $gid, $password, $akeys, $ensure=present) { user{ $name : ensure => $ensure, managehome => true, uid => $uid, gid => $gid, password => $password, groups => [''wheel''], require => Group[$name], } group { $name : ensure => $ensure, gid => $gid, } multi_ssh_authorized_key { $akeys: ensure => $ensure, username => $name, } } define multi_ssh_authorized_key ($ensure, $username) { ssh_authorized_key { $name: name => md5($title), # a shorter name ensure => $ensure, key => $title, type => ''ssh-rsa'', user => $username, require => User[$username], } } } so now a user definition can look like @ssh_user { ''martin.langhoff'': uid=> 2000 , gid => 2000, password => ''$6$gaga.'', akeys => [ ''onekey'' , ''anotherkey'' ] } and it all works. thank you! m -- martin.langhoff@gmail.com - ask interesting questions - don''t get distracted with shiny stuff - working code first ~ http://docs.moodle.org/en/User:Martin_Langhoff -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.