Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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
Linux Administration Cookbook

You're reading from   Linux Administration Cookbook Insightful recipes to work with system administration tasks on Linux

Arrow left icon
Product type Paperback
Published in Dec 2018
Publisher Packt
ISBN-13 9781789342529
Length 826 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Adam K. Dean Adam K. Dean
Author Profile Icon Adam K. Dean
Adam K. Dean
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Introduction and Environment Setup FREE CHAPTER 2. Remote Administration with SSH 3. Networking and Firewalls 4. Services and Daemons 5. Hardware and Disks 6. Security, Updating, and Package Management 7. Monitoring and Logging 8. Permissions, SELinux, and AppArmor 9. Containers and Virtualization 10. Git, Configuration Management, and Infrastructure as Code 11. Web Servers, Databases, and Mail Servers 12. Troubleshooting and Workplace Diplomacy 13. BSDs, Solaris, Windows, IaaS and PaaS, and DevOps 14. Other Books You May Enjoy

Using Vagrant to automatically provision VMs

Going through the tedium of installing a new VM every time you want to test something new, or create a sandbox to work in, can get old fast.

Because of this, various administrators and developers have come up with solutions that make provisioning a VM (or several) a breeze.

If we take a moment to think about the advantages of this approach, it's easy to highlight just a few benefits of automated VM provisioning:

  • It eliminates the time it takes to manually type answers into a VM window.
  • It allows for the automated running of software tests in a development environment.
  • It allows for the sharing of text files that act as recipes for how to build a VM, rather than the shifting of large VM images from station to station. This is a form of Infrastructure as Code (IaC).

Kickstart

One method of automating the deployment of boxes are kickstart files, which are frequently used in large deployments to automatically answer the questions that the installer asks the user.

If you take a look in the /root/ folder of a CentOS VM, there's a good chance you'll find a file called anaconda-ks.cfg, which is effectively the kickstart file for the manual steps you took when installing the machine (anaconda being the name of the installer).

These files are tweaked, or written from scratch, and then hosted on a web server, on an installation network, ready to be picked up by an unconfigured machine.

Vagrant

Locally, kickstart files aren't really practical, and they're not quick to work with. We need something that can be set up quickly and easily, but which is also very powerful.

Enter Vagrant.

Developed by Hashicorp as an open source piece of software, Vagrant can be used for automatically provisioning VMs, and even whole development environments. 

Typically, somewhere, you might find a Vagrantfile (the name of the core Vagrant... file...) is in a repository of some in-house application.

The developers working on the application pull down the repository to their local machine, and use the Vagrant configuration file to spin up a local development environment, which they can then use to test code changes or feature additions without utilizing expensive development environments.

Vagrant is available for macOS, Linux, and Windows.

On my Ubuntu host, I install Vagrant like so:

$ sudo apt install vagrant

There's quite a few dependencies, totalling around 200 MB of used disk space afterwards.

Ubuntu's packages are reasonably up to date, so we get a recent version:

$ vagrant --version
Vagrant 2.0.2

I'm quite particular about where I keep my files, so I'm going to create a dedicated folder called Vagrant in my home directory, which I'll use for working with my Vagrant VMs:

$ ls
Desktop Downloads Pictures snap Videos
Documents Music Public Templates 'VirtualBox VMs'
$ mkdir Vagrant
$ cd Vagrant/

Next, we will initialize a new Vagrantfile. The following command will do this automatically:

$ vagrant init
$ ls
Vagrantfile

Have a look in the Vagrantfile, but don't make any changes yet. You'll see that a lot of the options are listed, but commented out by default. This is a good way of introducing you to what Vagrant is capable of.

Note that, by default, Vagrant will attempt to use a box called base, but will also prompt you to look at https://vagrantcloud.com/search for other boxes:

  # Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "base"

Doing a search for CentOS on vagrantcloud reveals a nice default box we can use: https://app.vagrantup.com/centos/boxes/7.

It also lists the providers that the box can be provisioned under. VirtualBox is one of them, meaning it will work in our installation.

We need to change our Vagrantfile to point at this box. From the folder in which your Vagrantfile exists, run the following:

$ sed -i 's#config.vm.box = "base"#config.vm.box = "centos/7"#g' Vagrantfile

We've just used sed (a common tool for editing text on the command line, either in files or on standard out) with the -i option, to modify our Vagrantfile in-place. Opening the file now will show us that the base line has changed to point to centos/7 instead.

Now, we can provision our VM with another simple command:

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://vagrantcloud.com/centos/7
==> default: Adding box 'centos/7' (v1804.02) for provider: virtualbox
default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1804.02/providers/virtualbox.box
==> default: Successfully added box 'centos/7' (v1804.02) for 'virtualbox'!
<SNIP>
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /home/adam/Vagrant/ => /vagrant

All being well, your VM image will start to download from vagrantcloud, and your box will spin itself up in VirtualBox.

We can even see our VM in the VirtualBox main window:

Taking a look under Settings | Network and Port Forwarding shows how Vagrant also automatically sets up access for the NAT's network, in a very similar way to the manual way we did.

We can also connect to our new VM using a built-in Vagrant shortcut:

$ vagrant ssh
Last login: Tue Aug 7 09:16:42 2018 from 10.0.2.2
[vagrant@localhost ~]$

This means that we've provisioned and connected to a VM in four commands, in summary:

$ vagrant init
$ sed -i 's#config.vm.box = "base"#config.vm.box = "centos/7"#g' Vagrantfile
$ vagrant up
$ vagrant ssh
[vagrant@localhost ~]$

We can also destroy any VMs we create from within the same folder that we ran against our Vagrantfile using one command:

$ vagrant destroy
I wrote about manually setting up the VM with VirtualBox (and took all those screenshots) first, because it's good to get into the habit of learning about how things are done manually prior to automating the tedious bits. This same rule can be applied to most software, because even when it takes longer, knowing how something works under the hood makes troubleshooting much easier later on.
You have been reading a chapter from
Linux Administration Cookbook
Published in: Dec 2018
Publisher: Packt
ISBN-13: 9781789342529
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 $19.99/month. Cancel anytime
Banner background image