Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Puppet 5 Cookbook

You're reading from   Puppet 5 Cookbook Jump start your Puppet 5.x deployment using engaging and practical recipes

Arrow left icon
Product type Paperback
Published in Jun 2018
Publisher Packt
ISBN-13 9781788622448
Length 394 pages
Edition 4th Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Thomas Uphill Thomas Uphill
Author Profile Icon Thomas Uphill
Thomas Uphill
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Puppet Language and Style FREE CHAPTER 2. Puppet Infrastructure 3. Writing Better Manifests 4. Working with Files and Packages 5. Users and Virtual Resources 6. Managing Resources and Files 7. Managing Applications 8. Servers and Cloud Infrastructure 9. External Tools and the Puppet Ecosystem 10. Monitoring, Reporting, and Troubleshooting 11. Other Books You May Enjoy

Installing, configuring, and starting a service

There are many examples of this pattern online. In our simple example, we will create an Apache configuration file under /etc/httpd/conf.d/cookbook.conf. The /etc/httpd/conf.d directory will not exist until the httpd package is installed. After this file is created, we would want httpd to restart to notice the change; we can achieve this with a notify parameter.

How to do it...

We will need the same definitions as our last example; we need the package and service installed. We now need two more things. We need the configuration file and index page (index.html) created. For this, we follow these steps:

  1. As in the previous example, we ensure the service is running and specify that the service requires the httpd package:
service {'httpd':
ensure => running,
require => Package['httpd'],
}
  1. We then define package as follows:
package {'httpd':
ensure => installed,
}
  1. Now, we create the /etc/httpd/conf.d/cookbook.conf configuration file; the /etc/httpd/conf.d directory will not exist until the httpd package is installed. We'll use @heredoc syntax here to make the code a little more readable, assigning the cookbook.conf contents to the $cookbook variable. The require metaparameter tells Puppet that this file requires the httpd package to be installed before it is created:
$cookbook = @(COOKBOOK)
<VirtualHost *:80>
Servername cookbook
DocumentRoot /var/www/cookbook
</VirtualHost>
| COOKBOOK
file {'/etc/httpd/conf.d/cookbook.conf':
content => $cookbook,
require => Package['httpd'],
notify => Service['httpd'],
}
  1. We then go on to create an index.html file for our virtual host in /var/www/cookbook. Again, we'll use @heredoc syntax to make this more readable. This directory won't exist yet, so we need to create this as well, using the following code:
$index = @(INDEX)
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
| INDEX
file {'/var/www/cookbook':
ensure => directory,
require => Package['httpd'],
}
file {'/var/www/cookbook/index.html':
content => $index,
require => File['/var/www/cookbook'],
}

How it works...

The require attribute to the file resources tells Puppet that we need the /var/www/cookbook directory created before we can create the index.html file. The important concept to remember is that we cannot assume anything about the target system (node). We need to define everything on which the target depends. Anytime you create a file in a manifest, you have to ensure that the directory containing that file exists. Anytime you specify that a service should be running, you have to ensure that the package providing that service is installed.

In this example, using metaparameters, we can be confident that no matter what state the node is in before running Puppet, after Puppet runs, the following will be true:

  • httpd will be running
  • The VirtualHost configuration file will exist
  • httpd will restart and be aware of the VirtualHost file
  • The DocumentRoot directory will exist
  • An index.html file will exist in the DocumentRoot directory
You have been reading a chapter from
Puppet 5 Cookbook - Fourth Edition
Published in: Jun 2018
Publisher: Packt
ISBN-13: 9781788622448
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime