the correct way to pass values into modules

asked 2016-12-12 12:40:39 -0600

erichymowitz gravatar image

Greetings.

I'm a puppet novice and I'm using a very old version, so bear with me.

I took a puppet training class, and if I remember correctly, I learned that the "proper" way to pass values into a class was to define the class thusly:

(/etc/puppet/modules/etchostfile/manifests/init.pp)
class etchostfile
(
  $hostfile
)
{
  file { $hostfile :
    ensure => file,
    source => blah blah $hostfile blah,
    path => '/etc/hosts',
  }
}

(/etc/puppet/manifests/site.pp)
class profiles::generic
(
  $hostfile = 'mainhosts.txt'
)
{
  class { 'etchostfile' :
    hostfile => $hostfile
  }
}
node machine1
{
  class { 'profiles::generic' :
  }
}
node machine2
{
  class { 'profiles::generic' :
    hostfile => 'otherhosts.txt'
  }
}

And machine1 would get the "mainhosts.txt" file defined in the profile, and machine2 would get the "otherhosts.txt" file defined in the node.

But these variable definitions are getting very long. I now have 11 such variables that are separately passed to 5 classes.

So my co-worker feels like, if we knew what we were doing, there should be way to open my /etc/puppet/manifests/site.pp file with some variable definitions

$hostfile = 'mainhosts.txt'

and then define my nodes

class profiles::generic
{
  include etchostfile
}
node machine1
{
  include profiles::generic
}
node machine2
{
  $hostfile = 'otherhosts.txt'
  include profiles::generic
}

and have the variables properly passed along to the underlying classes.

But we can't get that working consistently. It seems to work sometimes, but not all of the time, and we can't figure out a pattern or a way to replicate the issue on demand. It either works, or it doesn't and we can't fix it.

Can somebody give me a pointer?

edit retag flag offensive close merge delete

Comments

1

What version of Puppet are you running? The way typically that this is done nowadays is to use Hiera to specify a data file which can be used to automatically fill those parameters without putting it in site.pp.

smarlow gravatar imagesmarlow ( 2016-12-12 14:04:47 -0600 )edit

2.7.26 . We aren't currently running hiera, mostly because we don't want that many different places to have to go looking for config options. But it might be worth looking at, if I can remember how it works. :)

erichymowitz gravatar imageerichymowitz ( 2016-12-12 14:24:48 -0600 )edit
1

Global variables are considered bad (that's what your co-worker suggests). Instead, specify a default value for the `hostfile` parameter of your `etchostfile` class https://docs.puppet.com/puppet/latest/lang_classes.html#syntax [NB `host` type https://docs.puppet.com/puppet/latest/types/host.html ]

Kai Burghardt gravatar imageKai Burghardt ( 2016-12-12 21:16:34 -0600 )edit

Part of your trouble is that you are still using 2.7, where global variables was the 'norm' for that configuration. hiera became a part of puppet in the 3.x branch. I know not everyone can upgrade, but I would highly suggest upgrading to puppet 4.x and make use of the improvements to the tooling.

DarylW gravatar imageDarylW ( 2016-12-13 09:03:02 -0600 )edit