most idiomatic way to set resource defaults

29 views
Skip to first unread message

Matthew Pounsett

unread,
Jul 7, 2016, 11:04:31 PM7/7/16
to Puppet Users

I have a defined resource type (let's call it `widget`) which gets defined multiple times per customer.  Each customer has their own default configuration values for their widgets, and these are very rarely (but occasionally) overridden.   I'd like to minimize the amount of typing necessary when someone adds a new widget to the systems, so I'm thinking of a Hiera structure similar to this:

widget_defaults:
   customer1
:
     colour
: green
     size
: big
     outline
: vague
   customer2
:
     colour
: blue
     size
: big
     outline: crisp

widgets
:
   customer1
:
      foo
:{}
      bar
:
         colour
: violet
      baz
:{}
   customer2
:
      froop
:{}
      vroom
:{}
      blargh
:
         outline
: circular

This will require multiple hiera lookups inside the profile that instantiates the resources though, in order to merge the defaults with any override values.   Is there a more idiomatic way to deal with something like this?


Peter Faller

unread,
Jul 8, 2016, 8:27:26 AM7/8/16
to Puppet Users
Are you using the create_resources function to instantiate the resources? It takes a third argument containing default values:

$widgets = hiera('widgets', {})
$widget_defaults = hiera('widget_defaults', {})
create_resources(::widget, $widgets, $widget_defaults)

Matthew Pounsett

unread,
Jul 11, 2016, 2:26:42 PM7/11/16
to Puppet Users
I haven't go so far as to write that part yet, but I was thinking of something along those lines.   Because of the varying defaults, I would actually need to iterate over the customer names.  

$widgets = hiera('widgets', {})
$widget_defaults = hiera('widget_defaults', {})
$widgets.each |String $customer, Hash $data| { create_resources( ::widget, $widgets[$customer], $widget_defaults[$customer] }

The problem I can't solve with this approach is that which customer's widgets I'm creating is actually an important attribute of the widget type, as it relates to where files get created, etc.  This is why I was wondering if there was a better way to organize the data in Hiera.  I considered reformatting the 'widgets' dictionary into a list so that the customer name is an attribute, not a key:

$widgets = hiera('widgets', [])
$widget_defaults = hiera('widget_defaults', {})
$widget_defaults.each |String $customer, Hash $defaults| {
   $widget_data = $widgets.filter |$data| { $data['customer'] == $customer }
   create_resources( ::widget, $widget_data, $widget_defaults[$customer])
}

That gets repetitive and doesn't organize  as well, however.  

R.I.Pienaar

unread,
Jul 11, 2016, 2:31:45 PM7/11/16
to puppet-users
I have a bunch of examples @ https://www.devco.net/archives/2015/12/16/iterating-in-puppet.php
something there might help, best avoid create_resources in puppet 4 :)

Matthew Pounsett

unread,
Jul 12, 2016, 8:28:49 PM7/12/16
to Puppet Users


On Monday, 11 July 2016 10:31:45 UTC-4, R.I. Pienaar wrote:

 best avoid create_resources in puppet 4 :)

Why is that? 

Matt Zagrabelny

unread,
Jul 12, 2016, 8:31:53 PM7/12/16
to puppet...@googlegroups.com
I can't speak for R.I., but I believe puppet 4 has "first class"
looping constructs. Thus, create_resource "hacks" won't be necessary.

-m
Reply all
Reply to author
Forward
0 new messages