Am I using puppet contain correctly?

3.8k views Asked by At

I am trying to create a dependancy relationship so that the phpwebserver profile is run before the silex_api profile.

role:

class roles::dev{
    include profiles::phpwebserver
    include profiles::silex_api

    Class ['profiles::phpwebserver'] -> 
    Class ['profiles::silex_api']
}

silex_api profile:

class profiles::silex_api{

    class { '::silex' :
        package_version => '1.6.2',
    }


    class {'::composer' :
         command_name  => 'composer.phar',
         target_dir    => '/var',
         user          => 'root'
    }

    contain ::composer
    contain ::silex
}

phpwebserver profile:

class profiles::phpwebserver{

    class { '::apache': 
    default_vhost => false,
    conf_template => "apache/httpd.silex.conf.erb",
    service_name => "httpd",
    package_ensure => "2.2.15-39.el6", 
    service_enable => true,
    service_ensure => "running",
  }

  class {'::apache::mod::php':
    package_name => "php",
      path => "${::apache::params::lib_path}/libphp54-php5.so",
  }


  contain ::apache
  contain ::apache::mod::php
}

classes:

class silex{
class { 'silex::install': }
class { 'silex::service': } 
}

The phpwebserver profiles successfully runs first if I explicitly contain all classes inside silex_api as shown below:

class profiles::silex_api{          
    class { '::silex' :
        package_version => '1.6.2',
    }


    class {'::composer' :
         command_name  => 'composer.phar',
         target_dir    => '/var',
         user          => 'root'
    }

    contain ::composer
    contain ::silex::install
    contain ::silex::service

}

Now I need to contain all the subclasses which to me, doesn't seem right. Is there a better way of accomplishing this or is this the standard way to ensure dependancies are set?

2

There are 2 answers

0
kaizenCoder On BEST ANSWER

I believe this is the correct way as per this article.

Role:

class roles::dev{
      include profiles::phpwebserver
      include profiles::silex_api

      Class ['profiles::phpwebserver'] -> 
      Class ['profiles::silex_api']
}

Profile:

class profiles::silex_api{

      class { '::silex' :
            package_version => '1.6.2',
      }

      class {'::composer' :
            command_name  => 'composer.phar',
            target_dir    => '/var',
            user          => 'root'
      }

      contain ::composer
      contain ::silex

}

Module:

class silex (
   $package_name = $::silex::params::silex_package_name,
   $package_version,
   $release_version = "1",
) inherits ::silex::params
{

       class { 'silex::install' :
           package_name    => $package_name,
           package_version => $package_version,
           release_version => $release_version,
       }

       contain silex::install
       contain silex::config
}
2
kkamil On

You mess up with resources declaration. You should not mix resources-like and include-like declarations. So if you use contain do not declare class again by class keyword.

In my opinion your class roles::dev is properly defined. You should remove double declaration from your profiles:

silex_api profile:

class profiles::silex_api{
    contain composer
    contain silex
}

phpwebserver profile:

class profiles::phpwebserver{
    contain apache
    contain apache::mod::php
}

To provide parameters to contained classes use hiera.

You might also consider using include function instead of contain.