On Monday, January 14, 2013 9:57:54 AM UTC-6, Guy Heckman
wrote:>
> Pardon my newbish question, but is there a non-iterative way to compare
> whether an array is a subset of another array?
>
> Something like:
>
> class scratchpad {
>
> *$config_ciphers = [''aes128-ctr'' ,
''aes192-ctr'', ''aes256-ctr'']*
>
> if ! is_array($config_ciphers) or empty($config_ciphers) {
> fail(''"config_ciphers" parameter must be an array of
valid ciphers,
> containing at least one element. See base_sshd class documentation for a
> list of valid values.'')
> } else {
> if ! (*$config_ciphers in [ ''3des-cbc'',
''aes128-cbc'', ''aes192-cbc'',
> ''aes256-cbc'', ''aes128-ctr'',
''aes192-ctr'', ''aes256-ctr'',
> ''arcfour128'', ''arcfour256'',
''arcfour'', ''blowfish-cbc'',
> ''cast128-cbc''])* {
> fail(''"config_ciphers" parameter must be an array
of valid ciphers.
> See base_sshd class documentation for a list of valid values.'')
> } else {
> notify {''config_ciphers'':
> message => "set Ciphers
${$base_sshd::config_ciphers}",
> }
> }
> }
>
> }
>
> This particular bit produces the following error which tells me that
> Puppet ''in'' only supports string-to-array comparisons.
>
> info: Loading facts in /etc/puppet/modules/stdlib/lib/facter/root_home.rb
>> info: Loading facts in
>> /etc/puppet/modules/stdlib/lib/facter/facter_dot_d.rb
>> info: Loading facts in
>> /etc/puppet/modules/stdlib/lib/facter/puppet_vardir.rb
>> info: Loading facts in
/etc/puppet/modules/stdlib/lib/facter/pe_version.rb
>> err: Could not retrieve catalog from remote server: Error 400 on
SERVER:
>> ''aes128-ctraes192-ctraes256-ctr'' from left operand of
''in'' expression is
>> not a string at /etc/puppet/manifests/scratchpad.pp:11 on node
>> puppetmaster.localdomain
>> warning: Not using cache on failed catalog
>> err: Could not retrieve catalog; skipping run
>>
>
> Showing how to approach this is appreciated, but links to documentation or
> a post that explains the finer points are preferred.
>
>
The docs for that operator indeed specify that its left operand must be a
string
(http://docs.puppetlabs.com/puppet/3/reference/lang_expressions.html#in).
Even if the operator accepted an array as its left operand, I would not
expect it to behave as you hope. There is a big difference between array A
being an element of array B on the one hand, and *every element* of A being
an element of B on the other.
As far as I know there is no built-in function or operator implementing the
kind of "contains all" test you want, but it should be pretty easy to
implement, either as a custom function or via Puppet''s Swiss army
knife,
inline_template(). For example:
$config_ciphers = [''aes128-ctr'' ,
''aes192-ctr'', ''aes256-ctr'']
$known_ciphers = [ ''3des-cbc'', ''aes128-cbc'',
''aes192-cbc'',
''aes256-cbc'', ''aes128-ctr'',
''aes192-ctr'', ''aes256-ctr'',
''arcfour128'', ''arcfour256'',
''arcfour'', ''blowfish-cbc'',
''cast128-cbc'']
if inline_template(''<%= @config_ciphers.all?{ |cipher|
@known_ciphers.member?(cipher) } ? "true" : "false"
%>'') != ''true'' {
# ...
}
Inline templates are basically a poor man''s custom function, as they
provide for you to embed arbitrary Ruby code in your manifests. Even with
the template being a one-liner, however, it would be better built as a
full-fledged custom function.
John
--
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/-/psa9Lqg8qloJ.
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.