Parameterized Class with Puppet

If you haven’t seen my previous tutorials you should see them:
How to install Puppet, Hello World module using template to Puppet,
Installing Apache and PHP with Puppet module, Installing Puppet master and slaves.

I’m using Xubuntu 12.04.03 32bit

This tutorial I’m creating parametrized class. Class is installing Apache2 and you can change Apaches port when running Puppet module.

Install Puppet, create folders and init.pp file.

$ sudo apt-get update && sudo apt-get -y install puppet
$ mkdir -p puppet/modules/apache2/manifests
$ cd puppet/
$ nano modules/apache2/manifests/init.pp

Create Apache2 class and in parameter it takes default port 80.

class apache2 ($port = '80') {
        package {'apache2':
                ensure => present,

        service {'apache2':
                ensure => "running",
                enable => "true",
                require => Package["apache2"],

        exec { "userdir":
                notify => Service["apache2"],
                command => "/usr/sbin/a2enmod userdir",
                require => Package["apache2"],

        file {'/etc/apache2/ports.conf':
                content => template("apache2/ports.conf.erb"),
                require => Package["apache2"],
                notify => Service["apache2"],

Create template folder and ports.conf.erb.

$ mkdir -p modules/apache2/templates
$ nano modules/apache2/templates/ports.conf.erb

Ports.conf.erb is copy of /etc/apache2/ports.conf, but $port ( in ports.conf.erb file <%= @port %> ) variable changes port what Apache is listening.

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default
# This is also true if you have upgraded from before 2.2.9-3 (i.e. from
# Debian etch). See /usr/share/doc/apache2.2-common/NEWS.Debian.gz and
# README.Debian.gz

NameVirtualHost *:80
Listen <%= @port %>

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443

<IfModule mod_gnutls.c>
    Listen 443

Run it without parameter. Apache should get installed and should work in localhost

$ sudo puppet apply --modulepath modules/ -e 'class {"apache2":}' 


Then run Puppet and add to 8080 to port parameter.

$ sudo puppet apply --modulepath modules/ -e 'class {"apache2": port => "8080",}'

Localhost now stop working.

Apache should listen localhost:8080.
Ok. We don’t want use that port now. So let’s change it back to 80.

$ sudo puppet apply --modulepath modules/ -e 'class {"apache2": port => "80",}'

Localhost:8080 doesn’t work no more.

Now Apache listens port 80.

You have installed Apache2 successfully and changed Apaches port from template using parametrized class.

Folder tree looks like this

└── modules
    └── apache2
        ├── manifests
        │   └── init.pp
        └── templates
            └── ports.conf.erb

This post is part of course