How to use class in different place

20 views
Skip to first unread message

Albert Shih

unread,
Aug 25, 2016, 4:40:25 PM8/25/16
to puppet...@googlegroups.com
Hi,

I would like to known how can I use a class in different place. Let's take
a example.

I have a module who manage a CMS, let's say something like drupal (or
whatever you want). So in this CMS I need to use apache class. So I get in
my

node.yaml

something like

classes :
- my_drupal_class

my_drupal_class::options::config1:
my_drupal_class::options::config2:
...
my_drupal_class::options::confign:


So now lets say I need a very simple vhost on that host. How can I do that
? If I load the class { apache } puppet going to complain because it's
already loaded inside my_drupal_class. If I don't load the class { apache }
I'm going to loose most of the params.pp (Like the version, the logpath
etc..)

Regards.

JAS



--
Albert SHIH
DIO bātiment 15
Observatoire de Paris
5 Place Jules Janssen
92195 Meudon Cedex
France
Téléphone : +33 1 45 07 76 26/+33 6 86 69 95 71
xmpp: j...@obspm.fr
Heure local/Local time:
jeu 25 aoū 2016 22:35:41 CEST

Henrik Lindberg

unread,
Aug 25, 2016, 6:25:39 PM8/25/16
to puppet...@googlegroups.com
On 25/08/16 22:40, Albert Shih wrote:
> Hi,
>
> I would like to known how can I use a class in different place. Let's take
> a example.
>
> I have a module who manage a CMS, let's say something like drupal (or
> whatever you want). So in this CMS I need to use apache class. So I get in
> my
>
> node.yaml
>
> something like
>
> classes :
> - my_drupal_class
>
> my_drupal_class::options::config1:
> my_drupal_class::options::config2:
> ...
> my_drupal_class::options::confign:
>
>
> So now lets say I need a very simple vhost on that host. How can I do that
> ? If I load the class { apache } puppet going to complain because it's
> already loaded inside my_drupal_class. If I don't load the class { apache }
> I'm going to loose most of the params.pp (Like the version, the logpath
> etc..)
>
> Regards.
>

The recommended approach is to always use 'include()' to include the
classes (you can include the same class any number of times). You then
use data binding (that is, automatic binding of class parameters to
values) by storing the parameters in hiera.

This way, all use of a class will get exactly the same parameter values
and the things being managed are only managed once. (As you noted you
cannot manage the same thing more than once using the resource like way
of creating a class where you can give it its parameter values even if
you were to give it exactly the same values).

Hope that helps.

- henrik

> JAS
>
>
>
> --
> Albert SHIH
> DIO bātiment 15
> Observatoire de Paris
> 5 Place Jules Janssen
> 92195 Meudon Cedex
> France
> Téléphone : +33 1 45 07 76 26/+33 6 86 69 95 71
> xmpp: j...@obspm.fr
> Heure local/Local time:
> jeu 25 aoū 2016 22:35:41 CEST
>


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Martin Alfke

unread,
Aug 26, 2016, 2:58:39 AM8/26/16
to puppet...@googlegroups.com
Hi Henrik,
> On 26 Aug 2016, at 00:25, Henrik Lindberg <henrik....@puppet.com> wrote:
>
>
> The recommended approach is to always use 'include()' to include the classes (you can include the same class any number of times). You then use data binding (that is, automatic binding of class parameters to values) by storing the parameters in hiera.

is include() still the recommended way?
Or should we start using contain()?

Best,
Martin

Luke Bigum

unread,
Aug 26, 2016, 4:20:14 AM8/26/16
to Puppet Users
Depends on if you want to ensure a class relationship or not. I would say containing classes in other modules is bad design, and soon you'll end up creating dependency loops. I would only ever contain a class that's in the same module, a common pattern would be:

class drupal {
  contain drupal::config
  contain drupal::install
  Class[drupal::install] -> Class[drupal::config]
}


To comment on the original poster's problem, if Class[My_drupal_class] is creating Class[Apache] using the resource like syntax, then Class[My_drupal_class] is not designed very well. In practice Drupal does not depend on Apache. It's a set of PHP files and a MySQL database, and it may or may not have Apache serve it's PHP (it could be another web server). If Class[My_drupal_class] is intended to be used under Apache, then it should create it's own Apache::Vhost resource (assuming Puppetlabs' Apache module) and that's it. Then somewhere else in your manifest, you will instantiate Class[Apache] with all the settings you want. This way you could even run other Apache services on the same Drupal machine, or, move Drupal to any other Apache server. Here's a sketch of a role/profile approach I would use:

class role::mywebserver {
   class { 'apache':
      all my options...
   }
   contain profile::drupal
   contain profile::some_other_apache_service
   Class[apache] -> Class[profile::drupal]
}

class profile::drupal {
   class { 'my_drupal_class':
      option       => 'something',
      parameter => 'something else',
      woof         => 'meow',
   }
   apache::vhost { 'my_drupal_vhost':
     listen           => 80,
     docroot        => '/opt/drupal',
     otherparams => 'I can't remember',
   }
}

In the above, profile::drupal is portable to any other role / node.

Albert Shih

unread,
Aug 26, 2016, 4:47:34 PM8/26/16
to puppet...@googlegroups.com
Le 26/08/2016 à 01:20:14-0700, Luke Bigum a écrit

Thanks for everything.
I will try that. I didn't known the « contain » methods. So until now I use
class or include, and the point is it's working with puppet 3.x (as
server), but I just switch to puppet 4.x (as server) and stop working.

Thanks.

Regards.



--
Albert SHIH
DIO bâtiment 15
Observatoire de Paris
5 Place Jules Janssen
92195 Meudon Cedex
France
Téléphone : +33 1 45 07 76 26/+33 6 86 69 95 71
xmpp: j...@obspm.fr
Heure local/Local time:
ven 26 aoû 2016 22:40:27 CEST
Reply all
Reply to author
Forward
0 new messages