Ask Your Question

About implementing classes

asked 2014-12-08 20:46:56 -0500

nitins gravatar image

In Pro Puppet, the author implements class as below without much explanation.

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

Isn't defining like below enough?

class ssh {
    include ::ssh::package
    include ::ssh::config
    include ::ssh::service

Ordering is handled within the sub-classes. So I am thinking it's not required here.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2014-12-09 11:29:17 -0500

reidmv gravatar image

Assuming relative ordering is handled within the sub-classes, this is almost enough. Instead though, to gain full equivalency (and in fact get something a little better), you should use contain.

class ssh {
    contain ::ssh::package
    contain ::ssh::config
    contain ::ssh::service

The last part of the original author's resource chaining, the class { '::ssh::service':} -> Class['ssh'] stanza, had to do with containment. Specifying that dependency made sure that anytime someone wrote something like require => Class['ssh'], the dependency would not be resolved until all of the classes contained in Class[ssh] were complete.

Consider the following code.

class example::one {
  notify { 'one': }

class example::two {
  include example::one

  notify { 'two a': }
  notify { 'two b': }

If you were to classify a node with example::two as written with the include function, it would produce a graph that had classes something like this.

image description

Note that there is no relationship at all declared between the two classes when include is used. The include keyword simply makes sure both exist somewhere in the graph.

In contrast, if example::two were modified to use the contain function as follows:

class example::two {
  contain example::one

  notify { 'two a': }
  notify { 'two b': }

The graph would end up having something like this in it:

image description

The containment relationship ensures that resources elsewhere in your code can specify dependencies like before => Class['ssh'] and require => Class['ssh'], and those dependencies will be relevant not just to the parent wrapper class, but also to each of the logical child classes you've defined (ssh::package, ssh::config, and ssh::service).

See also:

edit flag offensive delete link more


Thanks for the detailed explanation.

nitins gravatar imagenitins ( 2014-12-09 20:27:50 -0500 )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-12-08 20:46:56 -0500

Seen: 151 times

Last updated: Dec 09 '14