Ask Your Question
1

Best practice for distributing a bundle of related facts

asked 2013-07-31 12:17:20 -0600

barrkel gravatar image

updated 2013-07-31 12:18:07 -0600

I'm using Puppet 3.2.2 with Hiera as a lightweight ENC.

I have a bunch of related parameters, let's suppose they are DB settings: host, user, password, and name.

And I have a bunch of services, which may be on different nodes, and all these services need to know the DB settings.

So, after much reading, I've come up with 4 potential ways to distribute this info within the way things are set up:

  • Parameterize all the service-specific classes with 4 separate parameters, and use Puppet / Hiera autoconfiguration to fill in the values.

Example class

class ...
(more)
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
1

answered 2013-08-01 06:05:26 -0600

barrkel gravatar image

updated 2013-08-01 12:49:36 -0600

I've come up with a different approach.

I'm using hiera_hash to load a hash by key, and I'm passing in the key to look up as the parameter. I then pull values out of the hash as needed. This emulates the "structured parameters by reference" kind of behaviour I'm after.

For example, hiera yaml:

myapp_production_db:
  host: 'dbhost'
  name: 'myapp_production'
  user: 'myapp-user'
  password: 'myapp-password'
myapp::service1::db: 'myapp_production_db'
myapp::service2::db: 'myapp_production_db'
...

And in classes:

class myapp::service1(
  $db,
  ...
) {
  $db_params = hiera_hash($db)
  $db_host = $db_params['host'] # etc., or use hash lookup directly
}

This lets me switch the DB ... (more)

edit flag offensive delete link more

Comments

Some sample code and data might be a nice addition to your answer.

ramindk gravatar imageramindk ( 2013-08-01 10:29:33 -0600 )edit

Instead of repeating "myapp::serviceXXX::db 'my_app_production_db'" in your YAML file, you should be able to enumerate "service1", "service2", "serviceN" in a YAML array to be passed to a wrapper ...(more)

GregLarkin gravatar imageGregLarkin ( 2013-08-02 10:22:27 -0600 )edit

That will eliminate the need to embed service names (e.g. service1, service2) directly in your class names.

GregLarkin gravatar imageGregLarkin ( 2013-08-02 10:23:26 -0600 )edit

My class names have service names in them because they represent (singleton) service instances. They do not have homogenous parameter lists; i.e. they are not suitable for data-driven construction ...(more)

barrkel gravatar imagebarrkel ( 2013-08-02 12:56:05 -0600 )edit
1

answered 2013-08-02 09:38:05 -0600

gh gravatar image

The following design allows you to place the common things about your app, like the package, in the myapp class. You can pass this class a hash from hiera by setting it in myapp::services. Since you could have a variable number of services, this gets around the need for ::service1, ::service2, etc.

class myapp (
  $services = undef, # hash of different services
) {
  if $services != undef {
    create_resources('myapp::service',$services)
  }
}
define myapp::service (
  $db_host,
  $db_name,
  $db_user,
  $db_password,
  ...
) {
  ...
}
edit flag offensive delete link more

Comments

Perhaps there's been a misunderstanding; when I write service{1,2,3}, I do not mean I am emulating an array by embedding the index in the name. These ...(more)

barrkel gravatar imagebarrkel ( 2013-08-02 12:54:03 -0600 )edit

They all have other different arguments, the only commonality is the db settings, and perhaps the message queuing system settings, etc. depending on which service it is.

barrkel gravatar imagebarrkel ( 2013-08-02 12:54:45 -0600 )edit
1

answered 2013-07-31 13:05:53 -0600

GregLarkin gravatar image

Have a look at Hiera hashes and the create_resources function to accomplish this.

There is an excellent writeup in a previous question that should give you some ideas, and feel free to post follow-up questions here.

edit flag offensive delete link more

Comments

I see how create_resources instantiates a whole bunch of resources of a single type in a data-driven way, and how Hiera hashes can turn appropriately written yaml into a hash ...(more)

barrkel gravatar imagebarrkel ( 2013-08-01 04:19:44 -0600 )edit

I'm using hiera_include('classes') to drive creation of nodes; so the instantiation of classes per node is already data-driven, and the parameters are too. The issue is that the ...(more)

barrkel gravatar imagebarrkel ( 2013-08-01 04:22:59 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-07-31 12:17:20 -0600

Seen: 337 times

Last updated: Aug 02 '13