Book Image

Puppet 5 Cookbook - Fourth Edition

By : Thomas Uphill
Book Image

Puppet 5 Cookbook - Fourth Edition

By: Thomas Uphill

Overview of this book

Puppet is a configuration management system that automates all your IT configurations, giving you control of managing each node. Puppet 5 Cookbook will take you through Puppet's latest and most advanced features, including Docker containers, Hiera, and AWS Cloud Orchestration. Updated with the latest advancements and best practices, this book delves into various aspects of writing good Puppet code, which includes using Puppet community style, checking your manifests with puppet-lint, and learning community best practices with an emphasis on real-world implementation. You will learn to set up, install, and create your first manifests with version control, and also learn about various sysadmin tasks, including managing configuration files, using Augeas, and generating files from snippets and templates. As the book progresses, you'll explore virtual resources and use Puppet's resource scheduling and auditing features. In the concluding chapters, you'll walk through managing applications and writing your own resource types, providers, and external node classifiers. By the end of this book, you will have learned to report, log, and debug your system.
Table of Contents (16 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Preface

Configuration management has gone from something that few organizations practiced to a necessity. Today, every organization I visit is using configuration management in some form or another. The range of tools available has expanded as well. A few years ago, there were only two or three viable options for cross-platform configuration management. Now, there are many more options, including Salt, Chef, Ansible, and Puppet.

In the move to the cloud, it is important to be able to automate building nodes; manual tasks are no longer acceptable. Using Configuration management to automate the building of your nodes is now a requirement of the DevOps engineer.

This book takes you beyond the basics of Puppet and starts to explore the power of customizing Puppet to suit your needs. The book consists of mostly atomic recipes, which can be read independently to tackle a variety of real-world problems. The examples are structured so that you can dip in at any point and try out a recipe without having to work your way through the book from cover to cover. Code samples are included in each example. The book will take you from a rudimentary knowledge of Puppet to a more complete and expert understanding of Puppet's latest features and community best practices.

The open source version of Puppet was used throughout the book; you will not require Puppet Enterprise to complete the examples.

Puppet 5 is still changing rapidly, and I've included what I believe to be the most important parts of this release in this book. For up-to-the-minute information on the latest releases, I suggest you subscribe to the Puppet Developers mailing list at https://groups.google.com/forum/#!forum/puppet-dev, the Puppet Slack Channel at https://slack.puppet.com/, and the puppetlabs blog at https://puppet.com/blog.

Who this book is for

This book assumes a familiarity with Linux administration. The examples require some experience with command-line usage and basic text file editing. Although beneficial, previous coding experience is not required. This book is for the Puppet engineer that is looking for a book that covers a wide range of Puppet topics. This book is also for the system administrator looking for a guide to Puppet with real-world examples.

What this book covers

Chapter 1

,

Puppet Language and Style

,

 introduces the Puppet language and shows how to write manifests. The Puppet linting tool, puppet-lint, is introduced, and we review best practices to write Puppet code. Metaparameters are shown with examples. We also cover changes in the Puppet language available in versions 4 and 5 of Puppet.

Chapter 2

,

Puppet Infrastructure

,

 explains how to deploy Puppet in your environment. It covers the two main methods of installation, centralized and decentralized (masterless). It shows you how to use Git to centrally manage your code. It will help you configure PuppetDB and Hiera.

Chapter 3

,

Writing Better Manifests

,

 deals with organizing your Puppet manifests. Manifests are used to build modules. This chapter introduces the concept of roles and profiles to abstract how modules are applied to machines. Parameterized classes are introduced. It also shows you how to efficiently define resources with arrays of resources and resource defaults.

Chapter 4

,

Working with Files and Packages

,

 shows you how to manage files using snippets (fragments). It introduces the power of creating files with both Ruby (ERB) and Puppet (EPP) templates. It also helps you explore ways to secure information stored in your Puppet manifests.

Chapter 5

,

Users and Virtual Resources

,

 deals with the advanced topic of virtual and exported resources. Virtual resources are a way of defining resources but not applying them by default. Exported resources are similar but are used to have resources from one machine applied to one or more other machines.

Chapter 6

,

Managing Resources and Files

,

 speaks about dealing with directories and purging resources not controlled by Puppet. It shows you how to have file resources applied differently on different machines. Furthermore, methods for managing host entries in /etc/hosts are shown with exported resources examples.

Chapter 7

,

Managing Applications

,

 shows you how to use Puppet to manage your deployed applications. Using public Forge modules, it helps you configure Apache, NGINX, and MariaDB.

Chapter 8

,

Servers and Cloud Infrastructure

introduces virtual resources and shows how to use them to configure highly available services. It also shows you how to use Puppet to manage Docker and AWS instances. Furthermore, Vagrant is used as a tool to create test environments for your Puppet code.

Chapter 9

,

External Tools and the Puppet Ecosystem

,

 shows you how to extend Puppet with your own types and providers, how to make your own facts, as well as the Puppet Development Kit (PDK).

Chapter 10

,

Monitoring, Reporting, and Troubleshooting

,

 shows you how to leverage Puppet to monitor your infrastructure for problems. We configure Puppet reporting and then discuss some of the common problems with Puppet code.

To get the most out of this book

You will need a computer capable of running Linux virtual machines. The examples in the book use Debian and Enterprise Linux-based distributions (RHEL and CentOS). Some of the examples will require multiple virtual machines to be run concurrently. You will need a host machine capable of running three or four virtual machines simultaneously. A minimum of 8 GB of RAM is suggested for performance reasons. To configure a test environment similar to that used in the production of the book, Vagrant should be used. Vagrant is a tool developed by Hashicorp to enable quick deployment of virtual machines via a definition file,  Vagrantfile. More information on Vagrantfile syntax is available at https://www.vagrantup.com/docs/vagrantfile/Vagrantfile used to build the example systems in this book is provided in the files section. To effectively use the Vagrantfile provided, you will need to install the vagrant-hosts plugin. To do so, issue the following command:

[t@mylaptop ~] $ vagrant plugin install vagrant-hosts
Installing the 'vagrant-hosts' plugin. This can take a few minutes...
Fetching: vagrant-hosts-2.8.2.gem (100%)
Installed the plugin 'vagrant-hosts (2.8.2)'!

This will allow Vagrant to update the /etc/hosts file on your nodes with host entries for the other nodes in your test environment (for example, puppet.example.com and git.example.com). It is also convenient to configure ssh to connect to your test machines. After copying down Vagrantfile into a directory on your machine, cd into that directory. Check whether Vagrant is configured properly using the following command:

[t@mylaptop ~/cookbook] $ vagrant status
Current machine states:

cookbook poweroff (virtualbox)
puppet   running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

Now, copy the output of the following command into the .ssh/config file in your home directory as shown here:

[t@mylaptop ~/cookbook] $ vagrant ssh-config puppet >>~/.ssh/config

This will enable you to do the following:

[t@mylaptop ~/cookbook] $ ssh puppet
Last login: Mon Jun 18 20:13:21 2018 from 10.0.2.2
puppet.example.com
Managed by puppet 5.5.2
[vagrant@puppet ~]$ hostname
puppet.example.com

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Puppet-5-Cookbook-Fourth-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/Puppet5CookbookFourthEdition_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Apply this manifest using puppet apply git.pp; this will install Git."

A block of code is set as follows:

package {'git':
  ensure => installed
}

Any command-line input or output is written as follows:

t@mylaptop ~/.ssh $ ssh-copy-id -i git_rsa [email protected]
[email protected]'s password:
Number of key(s) added: 1

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.