Difference between include and require

1,347 views
Skip to first unread message

Douglas Garstang

unread,
Aug 23, 2011, 1:51:05 PM8/23/11
to Puppet Users
Can someone please succinctly explain to me the difference between include and require?

The documentation implies that simply putting 'require <class>' at the top of a different class automatically means that <class> is fully implemented as a dependency. However, it doesn't seem to work that way. Also, I'm not seeing a situation where the use of include seems to be automatically resolving dependancies. This is puppet 0.25.5.

Doug

David Kavanagh

unread,
Aug 23, 2011, 1:53:54 PM8/23/11
to puppet...@googlegroups.com
It seems there's a problem including a class multiple times. By
stating that one class requires another, you formalize the dependency,
but don't actually have an include. What you do then is to include the
class in your main implementation which uses that class. That way, if
you have several classes that depend on one other class, it is
included once at the top level, but used many places (where it is
required).
Is that as clear as I think it is?

David

> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To post to this group, send email to puppet...@googlegroups.com.
> To unsubscribe from this group, send email to
> puppet-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/puppet-users?hl=en.
>

Douglas Garstang

unread,
Aug 23, 2011, 6:10:59 PM8/23/11
to puppet...@googlegroups.com
Yes... that's what I had.... 
--
Regards,

Douglas Garstang
http://www.linkedin.com/in/garstang
Email: doug.g...@gmail.com
Cell: +1-805-340-5627

jcbollinger

unread,
Aug 24, 2011, 9:21:57 AM8/24/11
to Puppet Users


On Aug 23, 12:51 pm, Douglas Garstang <doug.garst...@gmail.com> wrote:
> Can someone please succinctly explain to me the difference between include
> and require?


The 'require' function (not to be confused with the 'require' resource
metaparameter) does everything that 'include' does, plus adds a class-
level dependency. Thus, this:

class foo {
require 'bar'
}

is equivalent to this:

class foo {
include 'bar'
}
Class['bar'] -> Class['foo']

To put it another way, 'include' affects only catalog compilation,
whereas 'require' affects both compilation by the master and
application by the agent.


> The documentation implies that simply putting 'require <class>' at the top
> of a different class automatically means that <class> is fully implemented
> as a dependency.


I'm not sure what you mean by that


> However, it doesn't seem to work that way. Also, I'm not
> seeing a situation where the use of include seems to be automatically
> resolving dependancies. This is puppet 0.25.5.


I can't really offer any advice based on that description of the
problem. Can you provide an example that demonstrates it?


John

jcbollinger

unread,
Aug 24, 2011, 9:32:37 AM8/24/11
to Puppet Users


On Aug 23, 12:53 pm, David Kavanagh <dkavan...@gmail.com> wrote:
> It seems there's a problem including a class multiple times. By
> stating that one class requires another, you formalize the dependency,
> but don't actually have an include. What you do then is to include the
> class in your main implementation which uses that class. That way, if
> you have several classes that depend on one other class, it is
> included once at the top level, but used many places (where it is
> required).
> Is that as clear as I think it is?


Not to me. To the extent that I understand what you're saying, it
doesn't sound right. The 'require' function does everything that
'include' does (and then some), so using 'require' vs. 'include' does
nothing to address any problems with multiple inclusion.

Furthermore, the issues with multiple inclusion are sufficiently mild
that my preferred coding practice is to rely on including classes
everywhere they are needed. To my knowledge, multiple inclusion is
problematic only in these cases:

1) When the included class uses a variable resolved via dynamic
scoping, and that variable may be resolved differently at different
points of inclusion. That doesn't bother me much, because if ever
that happens then it reflects a design error in the manifests anyway.
There are alternative, better ways to feed data to your classes than
dynamically scoped variables.

2) When the class to 'include' is parameterized (but I consider this
more a flaw in the design of the parameterized class framework than in
the 'include' function).


John
Reply all
Reply to author
Forward
0 new messages