Tuesday, December 11, 2012

Puppet require vs. include vs. class

According to puppet reference include and require:

  • Both include and require are functions;
  • Both include and require will "Evaluate one or more classes";
  • Both include and require cannot handle parametered classes
  • require is a superset of include, because it "adds the required class as a dependency";
  • require could cause "nasty dependency cycle";
  • require is "largely unnecessary"; See puppet language guide.
    Puppet also has a require function, which can be used inside class definitions and which does implicitly declare a class, in the same way that the include function does. This function doesn’t play well with parameterized classes. The require function is largely unnecessary, as class-level dependencies can be managed in other ways.
  • We can include a class multiple times, but cannot declare a class multiple times.
    class inner {
      notice("I'm inner")
    
      file {"/tmp/abc":
        ensure => directory
      }
    }
    
    class outer_a {
      # include inner
      class { "inner": }
    
      notice("I'm outer_a")
    }
    
    class outer_b {
      # include inner
      class { "inner": }
    
      notice("I'm outer_b")
    }
    
    include outer_a
    include outer_b
    
    Duplicate declaration: Class[Inner] is already declared in file /home/bewang/temp/puppet/require.pp at line 11; cannot redeclare at /home/bewang/temp/puppet/require.pp:18 on node pmaster.puppet-test.com
    
  • You can safely include a class, first two examples pass, but you cannot declare class inner after outer_a or outer_b like the third one:
    class inner {
    }
    
    class outer_a {
      include inner
    }
    
    class outer_b {
      include inner
    }
    
    class { "inner": }
    include outer_a
    include outer_b
    
    class { "inner": }
    class { "outer_a": }
    class { "outer_b": }
    
    include outer_a
    include outer_b
    class { "inner": } # Duplicate redeclaration error
    

1 comment:

  1. Thanks for telling us about puppet require and include functions..

    Also check : Super Gadget Factory

    ReplyDelete