Puppet method to subtract two arrays?

388 views
Skip to first unread message

Sean

unread,
Sep 17, 2015, 7:01:02 PM9/17/15
to Puppet Users
Hello,

I have been working on trying to drive an include statement with an array parameter.

The idea looks like so (in pseudo code) :


class foo( $whitelist = $::foo::params::whitelist, $blacklist = [],) {

  $include_list
= inline_template( "<% @whitelist - @blacklist %>" )
  validate_array
($include_list)
  include $include_list

}

I picked up that inline template from a message in the group dating back to 2011.

Basically, class foo has a ton of subclasses who's names populate the default value for $whitelist.  Normally all these get applied, but I need to allow for deviations for specific cases, thus the blacklist.

The issue seems to be that no matter what I put in whitelist or blacklist, include_list is always empty.  What am I missing?  I have also tried writing a Custom Function to do the same as the inline template but the result was not anymore successful, though the debugging was more difficult.

Thanks for your thoughts on this!

Peter Huene

unread,
Sep 17, 2015, 7:15:37 PM9/17/15
to puppet...@googlegroups.com
Hi Sean,

On Thu, Sep 17, 2015 at 12:01 PM, Sean <smal...@gmail.com> wrote:
Hello,

I have been working on trying to drive an include statement with an array parameter.

The idea looks like so (in pseudo code) :


class foo( $whitelist = $::foo::params::whitelist, $blacklist = [],) {

  $include_list
= inline_template( "<% @whitelist - @blacklist %>" )
  validate_array
($include_list)
  include $include_list

}

I picked up that inline template from a message in the group dating back to 2011.

The template is missing a '=' character to write the result, like so:

<%= @whitelist - @blacklist %>

However, this will return a string-ified version of the array and is not what you want as include will treat it as a single class name.

In the current version of the Puppet language, two arrays can simply be subtracted from one another:

$whitelist = [foo, bar, baz]
$blacklist = [bar]
include $whitelist - $blacklist

This would include "foo" and "baz", but not "bar".


Basically, class foo has a ton of subclasses who's names populate the default value for $whitelist.  Normally all these get applied, but I need to allow for deviations for specific cases, thus the blacklist.

The issue seems to be that no matter what I put in whitelist or blacklist, include_list is always empty.  What am I missing?  I have also tried writing a Custom Function to do the same as the inline template but the result was not anymore successful, though the debugging was more difficult.

Thanks for your thoughts on this!

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/6ac9c22b-f5fc-409b-a75e-7d286862220b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--
Peter Huene
Software Engineer, Puppet Labs
Puppet Open Source Team
-------------------------------------------

PuppetConf 2015
 
 is right around the corner! Join us October 5-9 in Portland, OR. Register now
.

Sean

unread,
Sep 17, 2015, 8:16:16 PM9/17/15
to Puppet Users
Hi Peter,

Thanks for that!  I must have lost the = in translation from the old post, I appreciate you 2nd set of eyes.  Now that I'm passed that and have data in the result, I've only graduated to having incorrect results.  I had used the any2array function to ensure the array status of the template output and the blacklist param.  The blacklist param will be delivered from and ENC (specifically Foreman), and while it should work, I've had issues in the past with arrays and hashes being taken as strings.  That said, here's what I see now:

$whitelist = [ foo::a, foo::b, foo::c, foo::d, foo::e ]
$blacklist = [ foo::c, foo::e ]

< apply corrected code, sending notifies for $whitelist, $blacklist, and $include_list >

The notify outputs show that $include_list is identical to $whitelist.  If I actually attempt the include $include_list statement (which is usually commented) I get duplicate resource definition errors since the blacklisted classes create resources like package { 'apache': ensure => absent } and the node's purpose might include being a webserver so we have another class which manages the package resource for apache.

I've experimented with the parameter from the ENC, and how I define whitelist in params.pp.  I have tried several experiments with quoting the array elements, with singles, with doubles, not quoting the elements, and even defining them as strings, and allowing any2array to convert them before the template.  The outputs are different each time, but the result is the same.  The blacklist items still appear in the end result.

FWIW, I'm running puppet 3.6.2 on the agent and 3.8.2 on the master, I assume you refer to current as the version 4 tree. 

Sean

unread,
Sep 18, 2015, 2:59:15 PM9/18/15
to Puppet Users
My woes were definitely due to the data in the parameters being treated as strings throughout.  Basically, for whatever reason even when using any2array(), the issue came down to all the desired individual elements were being treated as a single string that became element 0 of an array...

Effectively:
$whitelist = [ 'foo::a, foo::b, foo::c, foo::d, foo::e', ]
$blacklist = [ 'foo::c, foo::e', ]

It's possible that whitelist was not, but I can confirm that blacklist (sent to puppet via ENC) was indeed a string, that any2array converted to a single element array.  I have coerced the datatype in the ENC using Foreman's SmartClass parameter override, and am now getting the correct results.

I also converted the inline template into a custom function to avoid having to use any2array.

Angel L. Mateo

unread,
Sep 21, 2015, 6:42:53 AM9/21/15
to puppet...@googlegroups.com
Have you tried split function instead of any2array?

El 18/09/15 a las 16:59, Sean escribió:
> $include_list =inline_template("<% @whitelist -
> <https://groups.google.com/d/msgid/puppet-users/6ac9c22b-f5fc-409b-a75e-7d286862220b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
>
> --
> --
> Peter Huene
> Software Engineer, Puppet Labs
> Puppet Open Source Team
> -------------------------------------------
> *
> *
> *PuppetConf 2015 <http://2015.puppetconf.com/>
> ** is right around the corner! Join us October 5-9 in Portland,
> OR. **Register now <https://puppetconf2015.eventbrite.com/>
> **.*
>
> --
> 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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/8c70cabb-71c2-40a4-9081-ac0e10eb4ced%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/8c70cabb-71c2-40a4-9081-ac0e10eb4ced%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

--
Angel L. Mateo Martínez
Sección de Telemática
Área de Tecnologías de la Información
y las Comunicaciones Aplicadas (ATICA)
http://www.um.es/atica
Tfo: 868887590
Fax: 868888337
Reply all
Reply to author
Forward
0 new messages