Puppet roles/profiles, role with multiple instances of profile - how do parameters work?

Viewed 796

I am just learning Puppet (we have Puppet Enterprise locally). I am trying to understand the "roles and profiles" pattern. Please pardon any nomenclature slip ups.

How do I create a role with multiple instances of a profile, where the profile instances differ only by parameter? I'm guessing Hiera fits into this somewhere but I'm not exactly sure how.

For example:

Puppetfile:

mod 'puppetlabs-apache', '2.3.0'

apache.pp profile

class profile::apache (
    String $port = '80',
) {

  class { 'apache':
    listen => $port,
  }

}

twoapaches.pp role

class role::twoapaches {
  include profile::apache
  include profile::apache
}

I want an instance of the twoapaches role to have an apache at ports 90 and 100 - how do I do that?

2 Answers

You actually can't use classes like that in Puppet; a class can only be declared once-per-node.

You probably need some of the defined types in the puppetlabs/apache module. Defined types are used when you need to declare a user-defined "resource" more than once on a single node.

E.g. profile might be:

class profile::two_vhosts {
  apache::vhost { 'ip1.example.com':
    ip      => ['127.0.0.1','169.254.1.1'],
    port    => '80',
    docroot => '/var/www/ip',
  }
  apache::vhost { 'ip2.example.com':
    ip      => ['127.0.0.1'],
    port    => '8080',
    docroot => '/var/www/ip',
  }
} 

And the role might be:

class role::two_vhosts {
  include profile::two_vhosts
  include profile::other_stuff
  ...
}

If you needed to then pass the ports in, you might have:

class profile::two_vhosts (
  String $ip1_port,
  String $ip2_port, 
) {
  apache::vhost { 'ip1.example.com':
    ip      => ['127.0.0.1','169.254.1.1'],
    port    => $ip1_port,
    docroot => '/var/www/ip',
  }
  apache::vhost { 'ip2.example.com':
    ip      => ['127.0.0.1'],
    port    => $ip2_port,
    docroot => '/var/www/ip',
  }
} 

You could then have your role as:

class role::two_vhosts {
  class { 'profile::two_vhosts':
    ip1_port => '80',
    ip2_port => '8080',
  } 
  include profile::other_stuff
  ...
}

But in practice people use the Automatic Parameter Lookup feature here in conjunction with Hiera (ref).

Related