Configuración de máquinas virtuales usando Vagrant y Puppet

Vagrant es un programa escrito en Ruby que permite configurar varias máquinas virtuales y ejecutarlas transparentemente en la máquina host.

Se instala por apt-get install vagrant

1. Usa un box por cada máquina virtual. El box se descarga usando un comando como
<code>
vagrant box add lucid32 http://files.vagrantup.com/lucid32.box
</code>
que es almacenado en la ruta /home/asanchez75/.vagrant.d/boxes

2. Uno puede crear varios máquinas virtuales basadas en un solo box. De allí que es mejor crear una carpeta en
<code>
/home/asanchez75/Vagrant
</code>
y dentro de alli crear una carpeta por cada máquina Vagrant que creemos, por ejemplo para una llamada ubuntu sería
<code>
/home/asanchez75/Vagrant/ubuntu
</code>
3. Dentro de la carpeta
/home/asanchez75/Vagrant/ubuntu
ejecutamos el comando
<code>
vagrant init lucid32
</code>
Eso creará un archivo de configuración dentro de esta carpeta llamado Vagrantfile que será el que editemos para descargar los paquetes que necesitará mi máquina virtual vagrant.
Hay dos grandes repositorios para descargar estos paquetes, uno es Puppet y el otro es Chef.
Nosotros usaremos Puppet para este ejemplo.
Para ello creamos una carpeta dentro de '/home/asanchez75/Vagrant/ubuntu' llamada 'manifests' y dentro de esa carpeta creamos un archivo llamado 'lucid32.pp' con el siguiente contenido

<code>
class lucid32 {
exec { "apt_update":
command => "apt-get update",
path => "/usr/bin"
}

package { "php5":
ensure => present,
}

package { "libapache2-mod-php5":
ensure => present,
}

package { "apache2":
ensure => present,
}

service { "apache2":
ensure => running,
require => Package["apache2"],
}
}

include lucid32
</code>

Después, editamos el archivo de configuración 'Vagrantfile', que quedaría así
<code>
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::Config.run do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "lucid32"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
# config.vm.box_url = "http://domain.com/path/to/above.box&quot;

# Boot with a GUI so you can see the screen. (Default is headless)
# config.vm.boot_mode = :gui

# Assign this VM to a host-only network IP, allowing you to access it
# via the IP. Host-only networks can talk to the host machine as well as
# any other machines on the same network, but cannot be accessed (through this
# network interface) by any external networks.
# config.vm.network :hostonly, "33.33.33.10"

# Assign this VM to a bridged network, allowing you to connect directly to a
# network using the host's network device. This makes the VM appear as another
# physical device on your network.
# config.vm.network :bridged

# Forward a port from the guest to the host, which allows for outside
# computers to access the VM, whereas host only networking does not.
config.vm.forward_port 80, 4567

# Share an additional folder to the guest VM. The first argument is
# an identifier, the second is the path on the guest to mount the
# folder, and the third is the path on the host to the actual folder.
# config.vm.share_folder "v-data", "/vagrant_data", "../data"

# Enable provisioning with Puppet stand alone. Puppet manifests
# are contained in a directory path relative to this Vagrantfile.
# You will need to create the manifests directory and a manifest in
# the file lucid32.pp in the manifests_path directory.
#
# An example Puppet manifest to provision the message of the day:
#
# # group { "puppet":
# # ensure => "present",
# # }
# #
# # File { owner => 0, group => 0, mode => 0644 }
# #
# # file { '/etc/motd':
# # content => "Welcome to your Vagrant-built virtual machine!
# # Managed by Puppet.\n"
# # }
#
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "manifests"
puppet.manifest_file = "lucid32.pp"
end

# Enable provisioning with chef solo, specifying a cookbooks path (relative
# to this Vagrantfile), and adding some recipes and/or roles.
#
# config.vm.provision :chef_solo do |chef|
# chef.cookbooks_path = "cookbooks"
# chef.add_recipe "mysql"
# chef.add_role "web"
#
# # You may also specify custom JSON attributes:
# chef.json = { :mysql_password => "foo" }
# end

# Enable provisioning with chef server, specifying the chef server URL,
# and the path to the validation key (relative to this Vagrantfile).
#
# The Opscode Platform uses HTTPS. Substitute your organization for
# ORGNAME in the URL and validation key.
#
# If you have your own Chef Server, use the appropriate URL, which may be
# HTTP instead of HTTPS depending on your configuration. Also change the
# validation key to validation.pem.
#
# config.vm.provision :chef_client do |chef|
# chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME&quot;
# chef.validation_key_path = "ORGNAME-validator.pem"
# end
#
# If you're using the Opscode platform, your validator client is
# ORGNAME-validator, replacing ORGNAME with your organization name.
#
# IF you have your own Chef Server, the default validation client name is
# chef-validator, unless you changed the configuration.
#
# chef.validation_client_name = "ORGNAME-validator"
end
</code>
4. Ejecutamos el comando
<code>
vagrant up
</code>
para inicializar nuestra máquina virtual
5. Nos logueamos por ssh usando el comando
<code>
vagrant ssh
</code>
6. Dentro ya de nuestro servidor virtual reinicializamos el apache
7. Ubicamos las paginas que vayamos creando en nuestra maquina virtual usando
<code>
http://localhost:4567
</code>
8. Para parar el servicio usar el comando
<code>
vagrant halt
</code>
9. Si se desear borrar la máquina virtual y el box usar
<code>
vagrant destroy
</code>
Nota. Si el archivo 'Vagrantfile' contiene información sobre donde descargar el box y las demas configuraciones, entonces se puede borrar el box y cada vez que ejecutes
vagrant up
descargará el box y descargará los paquetes que hayas indicado.
En conclusión, solo necesitas tener instalado el gem vagrant y un buen archivo de configuración Vagrantfile para tener tu VM en cualquier sitio :)

Links de referencia para el post

http://unfoldthat.com/2011/05/06/using-vagrant-for-your-django-developm…
http://paperairoplane.net/?p=240
http://blog.mozilla.org/webdev/2011/10/04/developing-with-vagrant-puppe…
http://blog.smalleycreative.com/tutorials/setup-a-django-vm-with-vagran…
http://www.djangobook.com/en/2.0/
http://css.dzone.com/articles/creating-virtual-server

Etiquetas