[Puppet Users] Re: Hiera autolookup for defined types

jcbollinger
Tue, 06 Nov 2012 21:13:03 -0000

On Tuesday, November 6, 2012 11:04:46 AM UTC-6, Bruno Leon
wrote:
>
> Hello,
>
> since Puppet 3 hiera is doing auto variable lookup for classes, which in
> my
> view makes the code much clearer simply because we can use
>         include foo
> instead of
>         class { ''foo'':
>                 param1 => value,
>                 param2 => value,
>          }
>


There are other advantages to using ''include'', too, so bravo!


>
> However, AFAIK there is no such feature as auto looking parameters for
> defined
> type.
> A common case is multiple vhost in Apache.
>


You already started answering your own question when you wrote
"multiple".
For a similar feature to work for defined types, it has to be able to
identify keys to use to lookup parameters for a given instance, preferably
without risk of colliding with classes or other defined-type instances.


>
> Even though you can have apache installed as simply as "include
apache",
> you
> still have a lot of define (one per vhost), possibly each with different
> parameters.
>
> Would it be possible to have hiera looking up for a hash of parameters.
>
> Something like this in hiera:
>
> ---
> apache_vhost:
>         website1:
>                 web_param1: ''foo''
>                 web_param2: ''bar''
>         website2:
>                 web_param1: ''another_foor''
>                 web_param2: ''another_bar''
>
> And in the manifest, this would be invoked as simply as:
>
> node "test" {
>         include apache
>         apache_vhost { ''website1'': }
>         apache_vhost { ''website2'': }
> }
>
> Any ideas on this ? Any plans of implementation ?
>


You can do something very like what you describe via the built-in
create_resources() function.  Given the data exactly as you structured it
in your example, you could do this:

# load the vhost data as a hash of hashes
$vhost_data = hiera(''apache_vhost'')

# declare the apache_vhost instances
create_resources(''apache_vhost'', $vhost_data)

# (that''s it)

See
http://docs.puppetlabs.com/references/3.0.0/function.html#createresources
for more information.

Alternatively, you can write hiera lookups into your definition, either in
addition to or instead of parameters.  Or it can be more efficient to load
hiera data into class variables, and have your definition pull it from
there:

class apache::vhost_data {
  $data = hiera(''apache_vhost'')
}

define apache::vhost_wrapper () {
  include ''apache::vhost_data''

  $data = $apache::vhost_data::data[$name]
  apache_vhost { $name:
    web_param1 => $data[''web_param1''],
    web_param2 => $data[''web_param2'']
  }
}


John

--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To view this discussion on the web visit