Ask Your Question
2

What's the difference between include and contain?

asked 2017-06-16 05:39:10 -0500

clux gravatar image
  class ntp {
  file { '/etc/ntp.conf':
    ...
    require => Package['ntp'],
    notify  => Class['ntp::service'],
  }
  contain ntp::service
  package { 'ntp':
    ...
  }
}


class ntp {
  file { '/etc/ntp.conf':
    ...
    require => Package['ntp'],
    notify  => Class['ntp::service'],
  }
  include ntp::service
  package { 'ntp':
    ...
  }
}

What's the difference between include and contain? quite confusing.

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2017-06-18 22:04:58 -0500

reesek gravatar image

updated 2017-06-19 08:02:43 -0500

DarylW gravatar image

Any class that is declared via an include within another class may have their resources applied outside of the class from which they were declared.

Using your example from above, which I assume comes from here, let's break it down.

Personally, I think the documented example should be broken out into two different code blocks. As it reads, it appears it's as one, but it's really not, so I've separated them here for clarity. Let's start with the include behavior:

class ntp {
  file { '/etc/ntp.conf':
    ...
    require => Package['ntp'],
    notify  => Class['ntp::service'],
  }
  include ntp::service
  package { 'ntp':
    ...
  }
}

If we were to declare class ntp somewhere, as in the given docs example, like this:

include ntp
exec { '/usr/local/bin/update_custom_timestamps.sh':
  require => Class['ntp'],
}

the exec would happen after the file 'etc/ntp.conf' and the package 'ntp', but wouldn’t necessarily happen after the service. This is because resources declared within a class are always invoked within the surrounding class that declared them (in this case the surrounding class is the ntp class), but declared classes (IE ntp::service), and the resources within those declared classes MAY NOT be invoked before the surrounding class ends.

If resources declared inside a class (IE ntp::service) need to be guaranteed invoked within the surrounding class that declares it (IE ntp), then contain must be used. Then we'll ensure that the exec will happen after all the resources in both class ntp and class ntp::service. Why? Because ntp::service was contained.

You may need to read, and reread it over and over before it starts to sink in (as I have). contain, as I understand, was made as a replacement to the anchor pattern. Here is a youtube video that explains how resources declared within a class_a that are declared inside class_b via an include class_a can float off on their outside outside of the surrounding class.

If you don't get it right away, don't worry. This is a bit of an advanced topic for Puppet and it'll eventually gel.

Hope that helps!

-- Kris

edit flag offensive delete link more
0

answered 2017-06-19 00:07:01 -0500

clux gravatar image

@ reesek Thanks.

The page described 4 different ways to declare classes, however I can't distinguish them. They are all the same in my understanding. Sorry that I'm new to puppet.

edit flag offensive delete link more

Comments

no problem. I'd recommend focus on understanding the difference between an include-like vs a resource-like declaration as these two are the most you're likely to use. From there, require should be fairly straight forward. Just like anything else, as you get to using it, it'll all make sense!

reesek gravatar imagereesek ( 2017-06-19 09:58:51 -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

Stats

Asked: 2017-06-16 05:39:10 -0500

Seen: 69 times

Last updated: Jun 19