• No results found

Puppet Module Initialization Manifest (init.pp)

As shown in Listing 4-6, the manifest code that we’ve been working with would go into the init.pp file with a minor change.

Listing 4-6 The init.pp File for Our Apache Web Server Module

Click here to view code image class apache {

case $operatingsystem { centos: {

$webserver= ‘httpd’

$confpath = “/etc/$webserver/conf/$webserver.conf”

}

ubuntu: {

$webserver= ‘apache2’

$confpath = “/etc/$webserver/$webserver.conf”

}

default: {

fail(“Unsupported OS”) }

}

package { ‘apache’:

name => $webserver, ensure => installed, }

file {‘apacheconf’:

name => $confpath, ensure => file, mode => 600,

source => “puppet:///modules/apache/$webserver.conf”, require => Package[‘apache’],

}

service {‘apache’:

name => $webserver, ensure => running, enable => true,

subscribe => File[‘apacheconf’], }

}

The entire class definition will go into the init.pp file, which, in my default installation, will be placed in the /etc/puppetlabs/puppet/modules/apache/manifests/ directory.

Before we move on, let’s focus on the file resource. If you look at the source attribute, you’ll see a different path than we used previously. The path utilizes a special puppet URL format.

An additional benefit of using a module is that puppet treats your module path almost like a file server. The prefix puppet:///modules/apache/ tells puppet to look for the source file in the files subdirectory of the apache module found in your Puppet master’s modulepath (that is, /etc/puppetlabs/puppet/modules/apache/files). If you create a directory within the files directory, you just insert it in front of the filename, such as puppet:///modules/apache/subdirectory/$webserver.conf.

So, regardless of the system your manifest is running from, you do not need to worry about the full path of where you place files. Just place them in

/etc/puppetlabs/puppet/modules/apache/files/.

Templates

Before we wrap up our module discussion, let’s examine how to use templates to make our deployments a bit more flexible. We could use a single configuration file template instead of having different files for CentOS and for Debian. However, a discussion of the various options and Apache configuration file is beyond the scope of this book. Instead, we will work with a simpler example for our discussion, such as the content of an

index.html file. The default web page will tell our user what kind of operating system is running the web server. In practice, this is not the kind of information we would want publicized to malicious users. However, this simple example shows you how templates can prove useful.

First, we create a file called index.html.erb in our apache module’s templates directory, and it will contain the following content:

Click here to view code image

<html><body><h1>Puppet Rocks!</h1>

<p>This is the default web page for

<%# Print the correct Operating System name with the right article i.e. “a”

vs “an”%>

<% if @operatingsystem[0].chr =~/[AEIOU]/ %>

<%= “an” %>

<% else %>

<%= “a” %>

<% end %>

<b><%= @operatingsystem %></b> server.</p>

<p>The web server software is running and content has been added by your <b>Puppet</b> code.</p>

</body></html>

Note that there are different prefixes to the blocks of Ruby code:

<%= is for any values that you want output into the file that you are generating from the template (for example, the operating system name and the a or an article).

<%# is for comments to explain what the template code is supposed to be doing for anyone editing the file.

<% is for Ruby code such as conditional statements or loops. This proves useful if you want to output multiple lines of values based on entries in an array, list, hash, and so forth. The conditional statement that we are including in index.html.erb utilizes Ruby’s regular expression capabilities to verify whether the operating system’s name begins with a vowel or a consonant.

Next, we modify our apache class to have an additional file resource for the default web page. We will add a relationship metaparameter so that the HTML file isn’t deployed before the Apache web server is fully up and running. Also, we need to be aware of the path where the index.html file will be deployed because the default path differs for CentOS and Ubuntu. We will add a new variable called htmlpath to account for this, and our Puppet code will now look like the code sample in Listing 4-7.

Listing 4-7 Updated Apache Class with the index.html ERB Template

Click here to view code image class apache {

case $operatingsystem { centos: {

$webserver= ‘httpd’

$confpath = “/etc/$webserver/conf/$webserver.conf”

$htmlpath = “/var/www/html/index.html”

}

ubuntu: {

$webserver= ‘apache2’

$confpath = “/etc/$webserver/$webserver.conf”

$htmlpath = “/var/www/index.html”

}

default: {

fail(“Unsupported OS”) }

}

package { ‘apache’:

name => $webserver, ensure => installed, }

file {‘apacheconf’:

name => $confpath, ensure => file, mode => 600,

source => “puppet:///modules/apache/$webserver.conf”, require => Package[‘apache’],

}

service {‘apache’:

name => $webserver, ensure => running, enable => true,

subscribe => File[‘apacheconf’], }

file {‘apachecontent’:

name => $htmlpath, ensure => file, mode => 644,

content => template(‘apache/index.html.erb’), require => Service[‘apache’],

} }

There are a couple differences between using a static file and a template with a file

resource. With templates, we use the content attribute in the file resource instead of the source attribute. The content attribute is normally used to specify the actual content of the file. If we use the template keyword with the content attribute, Puppet will insert the content of the template file, as it processes the ERB code, into the target file.

The content attribute isn’t the only difference; the path that you specify with the template keyword differs as compared to the path used with the source attribute. The format is as follows:

Click here to view code image

<module name>/<template filename>

In our sample code, the module’s name is apache, and the template filename is

index.html.erb. The template file must be placed in the templates directory of the module’s path (/etc/puppetlabs/puppet/modules/apache/templates/).