Quick start » Adding classes (*nix)
Overview
Every module contains one or more classes. Classes are named chunks of Puppet code and are the primary means by which Puppet configures nodes. The puppetlabs-apache module you installed in the Module Installation Quick Start Guide contains a class called apache. In this example, you will:
- Use the
apacheclass to launch the default Apache virtual host - Edit class parameters in the main manifest
Prerequisites: This guide assumes you’ve already installed Puppet, and have installed at least one *nix agent and the puppetlabs-apache module.
Before starting this walk-through, complete the previous exercises in the introductory quick start guide. You should still be logged in as root or administrator on your nodes.
Add Apache to the main manifest
- From the command line of your Puppet master, navigate to the main manifest directory:
cd /etc/puppetlabs/code/environments/production/manifests. -
Use your text editor to open the
site.ppfile, and edit it so that it contains the following Puppet code:node default { include apache }Note: If you have already created the default node class, simply add
include apacheto it. Code from the Hello World! exercise does not need to be removed, but a class cannot be declared twice. We will explore this later in the guide. - Ensure that there are no errors in the Puppet code by running
puppet parser validate site.ppon the command line of your Puppet master. The parser will return nothing if there are no errors. If it does detect a syntax error, open the file and fix the problem before continuing. - From the command line of your Puppet agent, run
puppet agent -tto trigger a Puppet run.
Create the index.html file
- On the Puppet agent, navigate to
/var/www/html, and create a file calledindex.htmlif it does not already exist. - Open
index.htmlin your text editor and fill it with some content (for example, “Hello World”) or edit what is already there. - From the command line of your Puppet agent, run
puppet agent -t. -
Open a web browser and enter the IP address for the Puppet agent, adding port 80 on the end, as in
http://myagentnodeIP:80/.You will see the contents of
/var/www/html/index.htmldisplayed.
Editing class parameters in the main manifest
You can edit the parameters of a class in site.pp as well. Parameters allow a class to request external data. If a class needs to configure itself with data other than Puppet facts, provide that data to the class via a parameter.
To edit the parameters of the apache class:
- From the command line of your Puppet master, navigate to
/etc/puppetlabs/code/environments/production/manifests. - Use your text editor to open
site.pp. -
Replace the
include apachecommand with the following Puppet code:class { 'apache': docroot => '/var/www' }Note: You must remove
include apachebecause Puppet will only allow you to declare a class once.
That’s it! You have set the Apache web server’s root directory to
/var/wwwinstead of its default/var/www/html. If you refreshhttp://myagentnodeIP:80/in your web browser, it shows the list of files in/var/www. If you clickhtml, the browser will again show the contents of/var/www/html/index.html.Note: If you have Puppet Enterprise, you can do the steps in this guide through the PE web UI, the console.