In this chapter, we will create a virtualized environment suitable for learning and testing Puppet. We will utilize Vagrant and VirtualBox to set up this environment on your desktop or laptop. You can keep using this environment long after you have finished this book, or rebuild it quickly any time you’d like to test something new.
If you are an experienced developer or operations engineer, you are welcome to use a testing environment of your own choice. Anything that can host multiple Linux nodes will work. Puppet’s needs are minimal. Any of the following would be suitable for use as a Puppet test lab:
You can build your own test lab using one of the preceding solutions, or you can use an existing test lab you maintain. In all cases, I recommend using an OS compatible with RedHat Enterprise Linux 7 for learning purposes. The CentOS platform is freely available, and fully supported by both Red Hat and Puppet Labs. This will allow you to breeze through the learning exercises without distractions.
We recommend and are going to use Vagrant for the remainder of this book, for the following reasons:
If you plan to use your own testing environment, skip ahead to “Initializing Non-Vagrant Systems”.
If you are going to follow the recommendation to use Vagrant, let’s get started installing it. You’ll need to download two packages.
Go to VirtualBox Downloads and download the appropriate platform package for your system.
Next, go to Vagrant Downloads and download the appropriate platform package for your system.
Install these packages according to the instructions for your operating system. I’ve included detailed instructions for Windows and Mac users in the following subsections. If you’re running Vagrant on Linux or another platform, I’m going to assume you are expert enough to handle this installation yourself.
First, you should run the VirtualBox installer. Open the VirtualBox DMG image file you downloaded and click on the Virtualbox.pkg installer, as shown in Figure 2-1.
Accept the license and the installer will complete the installation.
Next, you should run the Vagrant installer. Open the Vagrant DMG image file you downloaded and click on the Vagrant.pkg installer, as shown in Figure 2-2.
Likewise, accept the license agreement and the installer will complete the installation.
Finally, if you don’t already have it, you will need to install Xcode on your machine. Perform the following steps:
xcode
into the search bar on the right.On Windows platforms, you must install Git and its tools in order to use Vagrant successfully.
Open your browser to Git Source Code Management and click the Windows Build on the lower right of the page. Run the installer when the download completes.
Accept the default components.
On the “Adjusting your PATH environment” screen, select “Use Git from the Windows Command Prompt,” as shown in Figure 2-3.
On the “Configuring the line ending conversions” screen (as shown in Figure 2-4), any option will work successfully for this book. Select the default option if you are unsure. You can change this later.
Select the MinTTY terminal emulator on the following screen (Figure 2-5).
Accept the final prompts to complete the installation.
Vagrant will use the VirtualBox virtualization engine to run the test systems. Installing VirtualBox is very straightforward. Run the VirtualBox installer package that you downloaded on the previous page.
Click Next to start the installation, and then click Next again to install all features (Figure 2-6).
You can disable the options to create shortcuts (Figure 2-7), as these won’t be necessary for the learning environment.
Accept the warning about a short interruption to networking (Figure 2-8). In most situations, you won’t notice any effect.
To install VirtualBox, click Install, as shown in Figure 2-9.
At this point, you’ll be prompted to install several virtualization drivers (Figure 2-10). You can either choose to always trust software from Oracle, or click Install for each one.
Disable the checkbox to start VirtualBox after installation (Figure 2-11). Vagrant will start it automatically.
Now we should install Vagrant. Run the Vagrant installer package that you downloaded (Figure 2-12).
You’ll need to accept the license agreement, as shown in Figure 2-13.
Select where you want to install Vagrant. As Figure 2-14 shows, I prefer to install Vagrant in the normal system location for 64-bit programs. It doesn’t matter which path you choose here.
Once you get to the confirmation screen acknowledging completion of the installation, you can exit the Setup Wizard by clicking Finish (Figure 2-15).
Windows systems will need to reboot, as shown in Figure 2-16.
At this point, you’ll need to start a Bash prompt. We’ll be using Bash for the remainder of this book, so now is a good time to become comfortable using it.
On a Macintosh, follow these steps:
On Windows 7 and before, follow these steps:
On Windows 8, follow these steps:
On Windows 10 (see Figure 2-17), follow these steps:
On Linux or any other platform, use the terminal prompt of your choice.
You may use any third-party terminal or command prompt that you prefer. Two popular choices are ConEmu for Windows and iTerm2 for Mac. Tabbed session support will be helpful, as you’ll log in to multiple instances at the same time in some exercises.
No matter which operating system you are using, the instructions assume you will start at a command prompt in your home directory.
To save some time later, you can download the virtual box image used as the base system for our learning environment (as mentioned before, we’ll use CentOS 7, as it is well supported by all Puppet Labs programs and modules):
$
vagrant
box
add
--provider
virtualbox
centos/7
=
=
>
box:
Loading
metadata
for
box
'centos/7'
box:
URL:
https://vagrantcloud.com/centos/7
=
=
>
box:
Adding
box
'centos/7'
(
v1707.01
)
for
provider:
virtualbox
box:
Downloading:
https://vagrantcloud.com/centos/boxes/7/versions/1707.01
=
=
>
box:
Successfully
added
box
'centos/7'
(
v1707.01
)
for
'virtualbox'
!
You may note that Vagrant Cloud contains many boxes that already have Puppet installed. For this book you will go through the installation of Puppet on a stock system so that you have that experience.
Now we’ll download a repository that contains virtual machine configurations and learning materials. Starting in your home directory, use git
to clone the following repository:
$
git
clone
https://github.com/jorhett/learning-puppet4
Cloning
into
'learning-puppet4'
...
remote:
Counting
objects:
238,
done
.
remote:
Total
238
(
delta
0
)
,
reused
0
(
delta
0
)
,
pack-reused
238
Receiving
objects:
100%
(
238/238
)
,
38.50
KiB
|
0
bytes/s,
done
.
Resolving
deltas:
100%
(
110/110
)
,
done
.
$
cd
learning-puppet4/
As shown, change into the learning-puppet4 directory. All further instructions will assume you are in this directory.
Now you should install the vagrant-vbguest
plugin. This plugin ensures that the VirtualBox extensions on the virtual machine are kept up to date.
$
vagrant
plugin
install
vagrant-vbguest
Installing
the
'vagrant-vbguest'
plugin.
This
can
take
a
few
minutes...
Fetching:
vagrant-share-1.1.9.gem
(
100%
)
Fetching:
micromachine-2.0.0.gem
(
100%
)
Fetching:
vagrant-vbguest-0.14.2.gem
(
100%
)
In particular, this plugin helps avoid problems that happen when a new kernel is installed, such as the /vagrant shared mount not being available.
The learning repository contains a Vagrantfile that lists the systems we’ll use in this book. If you are familiar with Vagrant, you’ll know that we can easily start these systems with vagrant up
. We’ll do that now to initialize a client system for learning Puppet:
$
vagrant
up
client
Bringing
machine
'client'
up
with
'virtualbox'
provider...
=
=
>
client:
Importing
base
box
'centos/7'
...
=
=
>
client:
Matching
MAC
address
for
NAT
networking...
=
=
>
client:
Checking
if
box
'centos/7'
is
up
to
date...
=
=
>
client:
Setting
the
name
of
the
VM:
learning-puppet4_client_1504376676610_6140
=
=
>
client:
Clearing
any
previously
set
network
interfaces...
=
=
>
client:
Preparing
network
interfaces
based
on
configuration...
client:
Adapter
1:
nat
client:
Adapter
2:
hostonly
=
=
>
client:
Forwarding
ports...
client:
22
(
guest
)
=
>
2222
(
host
)
(
adapter
1
)
=
=
>
client:
Running
'pre-boot'
VM
customizations...
=
=
>
client:
Booting
VM...
=
=
>
client:
Waiting
for
machine
to
boot.
This
may
take
a
few
minutes...
client:
SSH
address:
127.0.0.1:2222
client:
SSH
username:
vagrant
client:
SSH
auth
method:
private
key
client:
client:
Vagrant
insecure
key
detected.
Vagrant
will
automatically
replace
client:
this
with
a
newly
generated
keypair
for
better
security.
client:
client:
Inserting
generated
public
key
within
guest...
client:
Removing
insecure
key
from
the
guest
if
it
'
s
present...
client:
Key
inserted!
Disconnecting
and
reconnecting
using
new
SSH
key...
=
=
>
client:
Machine
booted
and
ready!
On a first boot the vbguest plugin will install the Virtualbox Guest Additions. This requires installing some CentOS packages we’ll skip over, but it should end like this:
Copy iso file VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso mount: /dev/loop0 is write-protected, mountingread
-only Installing Virtualbox Guest Additions 5.1.26 - guest version is unknown Verifying archive integrity... All good. Uncompressing VirtualBox 5.1.26 Guest Additionsfor
Linux........... VirtualBox Guest Additions installer Copying additional installer modules ... Installing additional modules ... vboxadd.sh: Starting the VirtualBox Guest Additions. Could not find the X.Org or XFree86 Window System, skipping. Redirecting to /bin/systemctl start vboxadd.service Redirecting to /bin/systemctl start vboxadd-service.service==
> client: Checkingfor
guest additions in VM...==
> client: Setting hostname...==
> client: Configuring and enabling network interfaces... client: SSH address: 127.0.0.1:2222 client: SSH username: vagrant client: SSH auth method: privatekey
==
> client: Rsyncing folder: /Users/jorhett/learning-puppet4/=
> /vagrant==
> client: Running provisioner: shell... client: Running: inline script
We’ve started only a single machine to learn from, named client. There are several other machines available that we will utilize in future chapters:
$
vagrant status Current machine states: client running(
virtualbox)
puppetmaster not created(
virtualbox)
puppetserver not created(
virtualbox)
dashboard not created(
virtualbox)
web1 not created(
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`
.
You can suspend, resume, and destroy these instances quite easily:
$
vagrantsuspend
client
==
> client: Saving VM state and suspending execution...$
vagrant resumeclient
==
> client: Resuming suspended VM...==
> client: Booting VM...==
> client: Waitingfor
machine to boot. This may take a few minutes... client: SSH address: 127.0.0.1:2222 client: SSH username: vagrant client: SSH auth method: private key client: Warning: Connection refused. Retrying...==
> client: Machine booted and ready!$
vagrant destroy client client: Are you sure you want to destroy the'client'
VM?[
y/N]
n
==
> client: The VM'client'
will not be destroyed, since theconfirmation
==
> client: was declined.
If you do destroy the instance, just run vagrant up client
to create a new instance and start over. We’re not going to spend any more time covering Vagrant here, but remember suspend
and resume
when you want to stop your test environment but don’t want to have to restart from scratch.
Now that this is running, let’s log in to the client system and get started.
$
vagrant ssh client[
vagrant@client ~]
$
If you are using Windows and a version of Vagrant below 1.7.5, it has a bug that prevents it from recognizing MinTTY properly. Upgrade to a more recent version.
If you are using Windows and not using Git Bash, you may get an error about SSH not being found. This is due to not having the Git binaries in your path. You can correct that for this command prompt like so:
C:
>
set
PATH
=
%PATH%
;
C:
P
rogram
Files
(
x86
)
G
it
in
The folder learning-puppet4 from your personal system should be synced to the client node as /vagrant. Let’s do a quick verification that it shows up here. You should see directory contents similar to this:
$
ls
/vagrant
bin
doc
etc-puppet
LICENSE
manifests
README.md
systemd-puppet
Vagrantfile
If you don’t see that, read back in your terminal to see if there was an error message about rsync of the /vagrant directory.
Unfortunately this isn’t a book about VirtualBox, so I feel obliged to send you to the VirtualBox support forum for a solution.
vagrant-vbguest
plugin as shown in “Install the Vagrant vbguest Plugin” and restarting the virtual machine.If you are using a virtual machine of your own choice, we’ll need to take a couple of steps so that you can follow the instructions in this book. If possible, mount the learning-puppet4/ directory to the virtual machine as /vagrant/. If you’re not sure how to do this, you can simulate it with the following commands:
$ git clone https://github.com/jorhett/learning-puppet4 … $ sudo ln -s /home/username/learning-puppet4 /vagrant
Throughout this book, you will see the following prompt shown in the examples:
[
vagrant@client ~]
$
You’ll need to mentally replace this with whatever your virtual node’s shell prompt is. If you wish to ensure it looks the same, you can use the following environment variable to sync them up:
$
export
PS1
=
'[vagrant@client W]$ '
Before we install Puppet on your virtual system, stop and take a moment to ensure you have a good text editor available to use. There are two ways to use text editors:
One issue for both Mac and Windows users is the handling of carriage returns and line feeds within files. Puppet and Git both work best when lines are terminated by line feeds without carriage returns, which is known as “Unix file format.” It is important to select an editor that can read and write files in this format without any surprises.
Many editors on Windows open Unix format files correctly, but will quietly replace the endings with a carriage return when saving. As your target nodes may expect line-feed–terminated files, this can cause file corruption on the target host. Likewise, the opposite problem can exist: if you are editing files to be written out to Windows nodes, then you may need to preserve the carriage returns in the file. If you have a mixed environment of Unix/Linux and Windows nodes, it is essential that your editor can handle both file formats.
The following subsections provide a list of editors we recommend. I have limited this list to only include editors that handle both file formats well. If you don’t have any Windows nodes, then you can use any editor you like.
There are three editors immediately available to you on the virtual system. All three of these normally create Unix format files, but can edit Windows format files without causing corruption. You can use the editors shown in Table 2-1 from the command line inside your virtual system.
Text editor | Command | Notes |
---|---|---|
Nano | sudo yum install nano |
An easy text editor for beginners—no book required! |
Vim | sudo yum install vim |
A powerful editor for experienced users; see Learning the vi and Vim Editors (O’Reilly). |
Emacs | sudo yum install emacs-nox |
A powerful editor for experienced users; see Learning GNU Emacs (O’Reilly). |
As it happens, all three of the aforementioned editors are installed on Macs by default, and available with the Cygwin package for Windows. You can get quite comfortable using these editors on your desktop.
When writing Puppet modules using a Windows system, you’ll run into problems with line endings within templates and source files. Windows uses both a carriage-return character and a line-feed character to end a line, whereas Mac, Linux, and Unix systems use only the line-feed character.
If you open up the files we use in this book with the Windows Notepad editor, you will see that they show up as a single, unbroken line. WorkPad can display the file, but will change the endings when it writes out changes. None of the built-in editors on Windows—Notepad, WordPad, or Word—are safe to use with Unix format files.
For Windows users, I highly recommend Notepad++. It can open and write out both Unix and Windows format files without changing the line endings. It does not reformat files unless you explicitly tell it to.
For Mac users, I recommend the TextWrangler and TextMate editors. The built-in TextEdit editor is minimally sufficient for Unix format files, but cannot handle files in Windows format properly.
If you are already a fan of the Unix editor Vim, you can find a GUI version of it at GVim or MacVim. Vim can safely read and write files in both formats.
If you have experience with the Eclipse IDE, the workspace editor can safely read, write, and convert upon request files in both Unix and Windows formats.
Use the following command to configure your editor of choice in the environment on the virtual machine (you’ll obviously need to set the editor to the one you want to use):
[
vagrant@client
~
]
$
echo
"EDITOR=nano"
>>
~/.bashrc
When following the instructions in this book, any time you see the word $EDITOR
in a command to type, you can either type this exactly or replace it with your editor. So, for example:
[
vagrant@client
~
]
$
$EDITOR
filename
would work exactly the same as:
[
vagrant@client
~
]
$
nano
filename
In this chapter, you created a virtualized environment suitable for learning and testing Puppet without affecting any production or personal systems.
The default learning environment we recommended had you install the Git development tools, Vagrant, and VirtualBox to provide a virtualized CentOS system on which you can test and develop.
If you already have a virtualization platform that you prefer to work with, you are welcome to use that instead. We recommended using an operating system compatible with RedHat Enterprise Linux 6 or 7, as it is the best tested and guaranteed version for compatibility with Puppet Supported modules that we will be using throughout the book. After you feel confident in the use of Puppet, there are detailed notes for use on other platforms in Appendix A.
During the course of this chapter, you cloned a Git repository that contains Vagrant system definitions, some example manifests, and other helpful files we’ll use throughout this book.
We also discussed the need for editing text files, and how you can use either an editor on the virtualized system or an editor on your desktop to create and edit Puppet manifests.
This environment is yours to keep. It will be useful as you develop and test Puppet manifests and modules during the learning process.