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?
Ah, but won't that imply a "to_s" on the resulting array of keys?
You may want to join the keys (say using ",") and split by that same
comma in the DSL. Pseudocode:
$keys = split(",", inline_template("<%= hash.keys.join(',') %>"))
Cheers,
Felix
As suggested by Felix:As suggested by Ohad:
(DOES work with 1 element tho)
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)
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).
> $keys = split(",", inline_template("<%= netifcfg.keys.join(',') %>"))
Honestly, I'd put this together as a Puppet function at this point, as
that's just resulting in something rather unmaintainable.
http://docs.puppetlabs.com/guides/custom_functions.html
I'll have a quick stab at it in a second.
module Puppet::Parser::Functions
newfunction(:hash_keys, :type => :rvalue) do |args|
unless args[0].is_a?(Hash)
Puppet.warning "hash_keys takes one argument, the input hash"
nil
else
args[0].keys
end
end
end
save to lib/puppet/parser/functions/ in a module.
--
Nigel Kersten
Product, Puppet Labs
@nigelkersten
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...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
I think this should be mainline *hint, hint* ;-)
Cheers,
Felix
> 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 have two errors. The first is that the split() function takes its
parameters in the other order from what Felix wrote. The string to
split on goes second, so you should do:
$keys = split(inline_template("<%= netifcfg.keys.join(',') %>"), ",")
The second error is that when you expand $keys within a string literal,
the elements of the array will be joined with nothing inbetween. Try
this instead:
notify {
$keys: ;
}
Or perhaps:
notify {
netifcfg-keys:
message => shellquote("Keys", "are:", $keys);
}
Those will show you better the real value of $keys.
/Bellman
I might take this as the first step towards collecting a bunch of
these in a module, and if there's enough interest in it during the
life of 2.7.x, we'll merge some of them into Telly, the next feature
release.