Hiera autolookup for defined types

1,557 views
Skip to first unread message

Bruno Léon

unread,
Nov 6, 2012, 12:04:18 PM11/6/12
to puppet...@googlegroups.com
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,
}

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

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 ?

Thanks
--
Bruno

jcbollinger

unread,
Nov 6, 2012, 4:13:03 PM11/6/12
to puppet...@googlegroups.com


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

Bruno Leon

unread,
Nov 7, 2012, 2:35:11 PM11/7/12
to puppet...@googlegroups.com

Thanks John for your very interesting reply.


On Tuesday, November 6, 2012 4:13:03 PM UTC-5, jcbollinger wrote:


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.

Actually the point was that the first key would be the defined type, which then has a "subkey" for each instance.
There is a risk of collision between defined type and class names, but that is already the case within Puppet language anyway.
 

This sound quite perfect for me, I'll give it a try !

Bruno
Reply all
Reply to author
Forward
0 new messages