Ask Your Question

Which way is better to implement classes?

asked 2014-07-20 02:40:35 -0600

spuder gravatar image

updated 2014-07-21 12:40:42 -0600

In the init.pp of manifests that I write, I usually include subclasses like so:

class ssh {
    include ::ssh::package
    include ::ssh::config
    include ::ssh::service
    Class['::ssh::package'] -> Class['::ssh::config'] -> Class['::ssh::service'}

Yet, on page 49 of the Pro Puppet book, second edition, the author recommends implementing classes like so:

class ssh {
  class { '::ssh::package': } ->
  class { '::ssh::config': } ->
  class { '::ssh::service': } ->

Both syntaxes work. What is the difference between them, and which is preferred?

Mostly I'm curious why the author recommends including the ssh class inside of itself.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2014-07-21 12:43:31 -0600

binford2k gravatar image

updated 2014-07-21 12:52:20 -0600

spuder gravatar image

There's not really much of a difference. The two things that differ are simply the different style of inclusion and the final dependency on the ::ssh class as the final line of the second example. (The upper case Class indicates that it is a reference, so it's not "including the ssh class inside of itself" as you say.)

The summary of the different inclusion styles is that the include function is roughly equivalent to this code, meaning that it can be called as many times as you like without duplication:

if ! defined(Class['::ssh::package'] {
  class { '::ssh::package': }

The second difference above is that the final dependency ( ... -> Class['::ssh'] ) is an attempt to halfway contain the sub-classes inside the ::ssh class. You can read more about containment at, but the summary of that is that a better solution for this example would be to use the new contain() function in place of include() and to not use that last dependency.

edit flag offensive delete link more


Good to know. I like the contain() function. According to the link, it is only available in puppet >= 3.4

spuder gravatar imagespuder ( 2014-07-21 13:06:22 -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


Asked: 2014-07-20 02:40:35 -0600

Seen: 142 times

Last updated: Jul 21 '14