title: Function contain() vs. Anchor Pattern in Puppet
body: <p>This post refers to <a href="https://stackoverflow.com/questions/24433335/puppet-require-not-working-as-expected">Puppet &quot;require&quot; not working as expected</a>.</p>
  
  <p>Is it possible to replace the Anchor Pattern with the function <code>contain</code> maintaining execution order and hinder declared classes of floating out. The two manifests look as follows:</p>
  
  <pre><code>class profile::maven inherits profile::base {
    # Hiera
    $version = hiera('profile::maven::version', '3.2.1')
    $settings = hiera_hash('profile::maven::settings', undef)
    $environments = hiera_hash('profile::maven::environments', undef)
  
    include 'profile::java'
  
    anchor { 'profile::maven::begin': }
  
    class { '::maven::maven': version =&gt; $version, }
  
    anchor { 'profile::maven::end': }
  
    if ($settings) {
      create_resources('::maven::settings', $settings)
    }
  
    if ($environments) {
      create_resources('::maven::environment', $environments)
    }
  
    Anchor['profile::maven::begin'] -&gt; Class['profile::java'] -&gt; Class['::maven::maven'] -&gt; Anchor['profile::maven::end']
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class profile::java inherits profile::base {
    # Hiera
    $distribution = hiera('profile::java::distribution', 'jdk')
    $version = hiera('profile::java::version', 'present')
  
    anchor { 'profile::java::begin': }
  
    class { '::java':
      distribution =&gt; $distribution,
      version      =&gt; $version,
    }
  
    anchor { 'profile::java::end': }
  
    # Parameters
    $java_home = $::java::java_home
  
    file { 'profile-script:java.sh':
      ensure  =&gt; present,
      path    =&gt; '/etc/profile.d/java.sh',
      content =&gt; template('profile/java.sh.erb'),
    }
  
    Anchor['profile::java::begin'] -&gt; Class['::java'] -&gt; File['profile-script:java.sh'] -&gt; Anchor['profile::java::end']
  
  }
  </code></pre>
  
  <p>Because of the current issue <a href="https://tickets.puppetlabs.com/browse/PUP-1597" rel="nofollow noreferrer">PUP-1597</a> in Puppet 3.6.x, the profile classes have to be renamed, otherwise we get <code>Error: undefined method 'ref' for nil:NilClass</code>. Applying the changes result in:</p>
  
  <pre><code>class profile::mavenp inherits profile::base {
    # Hiera
    $version = hiera('profile::maven::version', '3.2.1')
    $settings = hiera_hash('profile::maven::settings', undef)
    $environments = hiera_hash('profile::maven::environments', undef)
  
    include 'profile::javap'
  
    class { '::maven::maven': version =&gt; $version, }
    contain 'maven::maven'
  
    if ($settings) {
      create_resources('::maven::settings', $settings)
    }
  
    if ($environments) {
      create_resources('::maven::environment', $environments)
    }
  
    Class['profile::javap'] -&gt; Class['::maven::maven']
  
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class profile::javap inherits profile::base {
    # Hiera
    $distribution = hiera('profile::java::distribution', 'jdk')
    $version = hiera('profile::java::version', 'present')
  
    class { '::java':
      distribution =&gt; $distribution,
      version      =&gt; $version,
    }
    contain 'java'
  
    # Parameters
    $java_home = $::java::java_home
  
    file { 'profile-script:java.sh':
      ensure  =&gt; present,
      path    =&gt; '/etc/profile.d/java.sh',
      content =&gt; template('profile/java.sh.erb'),
    }
  
  }
  </code></pre>
  
  <p>Are these changes equivalent?</p>
  
  <p>If someone has a better idea of how to the deal with technologcial dependencies in Puppet using the profile/role approach, do not hesitate to share your thoughts.</p>