Skip to main content

Virtual Machine Automation with Vagrant

·6 mins

We have built ourselves a virtual machine that we can reuse for different purposes by duplicating them. But I am lazy and for me it does not feel kind of right when I want to build up my virtual infrastructure. I can only build it up once and have to go with it. I want to be more dynamic. Creating and destroying my infrastructure as often as I want to with a push of a button.

Gladly, there are some clever people over at Vagrant. Those guys are providing automation software for virtual machines. And you know what? We are going to check that out in this post.

So, let’s use our virtual machine that we have created in “Installing CentOS 6.2 with VirtualBox”.

We need a so called “base box”. The following steps will mostly repeat what has been provided in this HowTo, but I will present them here with my box-specific adaptions, if there are any. That, I don’t know yet.

Okay, let’s start VirtualBox with our minimal CentOS image.

Preconfiguration #

A few modifications have to be done in order to be able to create our base box.

First, I have deactivated the floppy and CD drive. We surely won’t need it in the future.

I have also disabled audio.

Remember network is set to NAT!

User and Rights Management #

When logged in as root, I have changed his password with

{% highlight none %} passwd {% endhighlight %}

I have also created the user vagrant and set his password to vagrant

{% highlight none %} useradd vagrant passwd vagrant {% endhighlight %}

Now, we need to make sure, that the main user vagrant has password-less sudo privileges. Startup your machine, and log in as vagrant and then type

{% highlight none %} su yum -y install sudo {% endhighlight %}

Next, we need to edit the file /etc/sudoers. Since we don’t have a decent text editor, get one with

{% highlight none %} yum -y install vim {% endhighlight %}

Now type

{% highlight none %} vim /etc/sudoers {% endhighlight %}

STOP! Don’t edit it this way. There is a special command for editing this file, just type

{% highlight none %} visudo {% endhighlight %}

Add the following line

{% highlight none %} %admin ALL=NOPASSWD: ALL {% endhighlight %}

By the way, keep vim, we’ll need it later, that’s for sure.

Now, we create a new group called admin and add the user vagrant to it.

{% highlight none %} groupadd admin usermod -a -G admin vagrant {% endhighlight %}

Test what you did by typing

{% highlight none %} sudo which sudo {% endhighlight %}

The output should be /usr/bin/sudo.

The documentation on the vagrant site says, that you need to restart sudo by typing /etc/init.d/sudo restart. Didn’t work for me … Said “No such file or directory”. But it works anyways. Computers, strange they are. You know what went wrong here?

VirtualBox Guest Additions #

Installing the VirtualBox guest additions is next. I have written about that in a former blog post when installing a desktop environment on our minimal CentOS installation. Here are the steps:

{% highlight none %} yum update kernel yum install gcc yum install kernel-devel yum install make export MAKE=’/usr/bin/gmake -i' mkdir /media/VirtualBoxGuestAdditions {% endhighlight %}

{% highlight none %} mount -r /dev/cdrom /media/VirtualBoxGuestAdditions sh /media/VirtualBoxGuestAdditions/VBoxLinuxAdditions.run {% endhighlight %}

You can safely ignore the failed installation of the windows system drivers since we won’t need them. Done with the guest additions.

Getting some more Software #

Let’s get some more software that we need later on.

We start with Ruby and RubyGems

{% highlight none %} yum install ruby yum install rubygems {% endhighlight %}

Yes, documentation is good. Let’s get the main source of information:

{% highlight none %} yum install man {% endhighlight %}

Additionally we need Puppet. More about why we need it will follow in an upcoming blog post. Installation with one command is not sufficient this time. We need to tell yum where to find it.

I basically followed this howto.

{% highlight none %} vim /etc/yum.repos.d/puppet.repo {% endhighlight %}

And add the following:

{% highlight none %} [puppetlabs] name=Puppet Labs Packages baseurl=http://yum.puppetlabs.com/el/$releasever/products/$basearch/ enabled=1 gpgcheck=1 gpgkey=http://yum.puppetlabs.com/RPM-GPG-KEY-puppetlabs {% endhighlight %}

Then we need the EPEL x86_64 yum repository:

{% highlight none %} rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm {% endhighlight %}

Install the puppet master packages and then the puppet client packages:

{% highlight none %} yum install puppet-server {% endhighlight %}

Just answer every question with “yes” to install everything.

SSH-Key Based Authentication #

Next we prepare everything for automatic ssh-key-based authentication.

{% highlight none %} cd mkdir .ssh chmod 700 .ssh touch .ssh/authorized_keys chmod 0600 .ssh/authorized_keys {% endhighlight %}

Now get the vagrant ssh keys. Put the private key (have renamed that to `id_rsa.vagrant) on your host system and the public key into your guest vm.

Get the keys from here.

Plus, set UseDNS to noin /etc/ssh/sshd_config to increase ssh performance on your host system.

You can copy the ssh-key by mounting the shared folder

On your host OS, copy the public key into the shared folder.

Then, in my virtual machine, I have mounted that share with

{% highlight none %} sudo mount -t vboxsf share /mnt {% endhighlight %}

Then add it to your file with the public keys

{% highlight none %} cat /mnt/vagrant.pub » /home/vagrant/.ssh/authorized_keys {% endhighlight %}

Finally type

{% highlight none %} visudo {% endhighlight %}

and change the line

{% highlight none %} defaults requiretty {% endhighlight %}

to

{% highlight none %} #Defaults requiretty {% endhighlight %}

Packing Up #

Let’s package everything up. Shut down your virtual machine and then run the following command in your native shell

{% highlight none %} vagrant package –base “CentOS Base Box” {% endhighlight %}

Let’s try our little box by creating a vagrant box in another directory:

{% highlight none %} mkdir test_environment cd test_environment vagrant box add my_test_box /<path_to_your_box>/package.box vagrant init my_test_box vagrant up {% endhighlight %}

After that command I got the following message (in red!):

{% highlight none %} SSH authentication failed! This is typically caused by the public/private keypair for the SSH user not being properly set on the guest VM. Please verify that the guest VM is setup with the proper public key, and that the private key path for Vagrant is setup properly as well. {% endhighlight %}

I definitely need to revisit the whole authentication issue.

To log into your box type

{% highlight none %} vagrant ssh {% endhighlight %}

Tataaaa. Am logged in my vagrant box.

Let’s destroy the whole thing and create it again when we need it next time.

{% highlight none %} vagrant destroy {% endhighlight %}

Done for today!