Re: [Puppet Users] "Listing" the keys in a hash as an array
Thanks guys I ended up breaking up what Felix&Ohad suggested into two lines to make it work: $keycsv= inline_template("<%= netifcfg.keys.join(',') %>") $keys = split ($keycsv,',') Yep, it's a 'hack' (sorry, Ohad, workaround ;)), but it saves me having to add puppet functions into SVN... (which is quite involved in my case because we support a big site with versioned modules and a custom module-versioning mechanism that at present does not cover custom functions (a long, painful topic in its own right) so I need to contain this code in the module using it - making this workaround perfect. Should we perhaps nudge the above function into mainstream puppet? Being able to list the keys in a hash is quite a crucial capability to have. -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Re: [Puppet Users] "Listing" the keys in a hash as an array
I must be doing something wrong... As suggested by Ohad: Manifest says: $keys = inline_template("<%= netifcfg.keys %>") exec { "/bin/echo keys are $keys and netifcfg is $netifcfg": logoutput => true } Output says: notice: /Stage[main]/Base::Network-common/Exec[/bin/echo keys are bond0bond1 and netifcfg is bond0ipaddress10.15.69.177netmask255.255.254.0defaultgatewayyesgateway10.15.68.1bond1ipadderss1.2.3.4netmask255.255.254.0gateway1.2.3.1]/returns: is notrun, should be 0 (noop) (DOES work with 1 element tho) As suggested by Felix: Manifest says: $keys = split(",", inline_template("<%= netifcfg.keys.join(',') %>")) exec { "/bin/echo keys are $keys and netifcfg is $netifcfg": logoutput => true } Output says: notice: /Stage[main]/Base::Network-common/Exec[/bin/echo keys are , and netifcfg is bond0ipaddress10.15.69.177netmask255.255.254.0defaultgatewayyesgateway10.15.68.1bond1ipadderss1.2.3.4netmask255.255.254.0gateway1.2.3.1]/returns: keys are , and netifcfg is bond0ipaddress10.15.69.177netmask255.255.254.0defaultgatewayyesgateway10.15.68.1bond1ipadderss1.2.3.4netmask255.255.254.0gateway1.2.3.1 (Split doesn't seem to work). -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Re: [Puppet Users] "Listing" the keys in a hash as an array
I must be doing something wrong... As suggested by Ohad: Manifest says: $keys = inline_template("<%= netifcfg.keys %>") exec { "/bin/echo keys are $keys and netifcfg is $netifcfg": logoutput => true } Output says: notice: /Stage[main]/Base::Network-common/Exec[/bin/echo keys are bond0bond1 and netifcfg is bond0ipaddress10.15.69.177netmask255.255.254.0defaultgatewayyesgateway10.15.68.1bond1ipadderss1.2.3.4netmask255.255.254.0gateway1.2.3.1]/returns: is notrun, should be 0 (noop) (DOES work with 2 element tho) As suggested by Felix: Manifest says: $keys = split(",", inline_template("<%= netifcfg.keys.join(',') %>")) exec { "/bin/echo keys are $keys and netifcfg is $netifcfg": logoutput => true } Output says: notice: /Stage[main]/Base::Network-common/Exec[/bin/echo keys are , and netifcfg is bond0ipaddress10.15.69.177netmask255.255.254.0defaultgatewayyesgateway10.15.68.1bond1ipadderss1.2.3.4netmask255.255.254.0gateway1.2.3.1]/returns: keys are , and netifcfg is bond0ipaddress10.15.69.177netmask255.255.254.0defaultgatewayyesgateway10.15.68.1bond1ipadderss1.2.3.4netmask255.255.254.0gateway1.2.3.1 -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
[Puppet Users] "Listing" the keys in a hash as an array
Hi all I've got node definition on the site with server interface, bonding and TCP/IP configurations inscribed in a per-node [ hash + accompanying array-of-interface-names ] (e.g. $array = [ 'eth0', 'eth1' ] $hash = { eth0 => { ipaddress => 1.2.3.4, ... } eth1 => { ipaddress => 5.6.7.8, ... } } What I have right now works, but is clunky and could probably be written better. My 2 questions are: 1. Can I somehow get a list of keys from the hash itself (a list that will behave like an array that I can feed into a defined type) instead of "manually" defining $array for this purpose? 2. If you look further down inside in this post, the code (taken from inside the base class) does this: [a] gets the hash with the configuration passed in as a global variable [b] copies the hash elements into local variables (because a local variable can be double-quoted around, and behave nicely even if it wasn't set and is blank, whereas a hash element cannot) [c] invokes the interface-configuration defined type with the local variables as parameters. A very messy roundabout way of doing things. Can anyone suggest an easier way (tho still ultimately has to call base::network-common::interface) In the node file I have a configuration block that looks like this: $netiflist = [ 'eth0' ] $netifcfg = { eth0 => { ipaddress => "1.2.3.4", netmask => "255.255.255.0", gateway => "1.2.3.1", defaultgateway => "yes" }, } include base Then in a subclass of base I have: class base-subclass { # Iterate for all the interfaces that need to be configured: if $netiflist { base::network-common::dointerfaces {[$netiflist]:}} } # the actual defined type: define base::network-common::dointerfaces() { # copy hash elements into local variables: $myipaddress = $netifcfg[$name][ipaddress] $mynetmask = $netifcfg[$name][netmask] $mygateway = $netifcfg[$name][gateway] $mydefaultgateway = $netifcfg[$name][defaultgateway] $myhwaddress = $netifcfg[$name][hwaddress] $myroutes = $netifcfg[$name][routes] $myensure = $netifcfg[$name][ensure] # and finally use the double-quoted local variables to call the interface defined type: base::network-common::interface { $name: ipaddress => "$myipaddress", netmask => "$mynetmask", gateway => "$mygateway", defaultgateway => "$mydefaultgateway", hwaddress => "$myhwaddress", routes => "$myroutes", ensure => "$myensure" } } ... -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.