Skip to main content

Vagrant: introducción, documentación y comandos básicos

Índice de recursos oficiales

Introduction to Vagrant
https://developer.hashicorp.com/vagrant/intro


Get Started (9 tutorials)
https://developer.hashicorp.com/vagrant/tutorials/getting-started/getting-started-index

  1. What is Vagrant?
  2. Install Vagrant
  3. Initialize a Project Directory
  4. Install and Specify a Box
  5. Boot an Environment
  6. Synchronize Local and Guest Files
  7. Share an Environment
  8. Rebuild an Environment
  9. Teardown an Environment

Networking and Provisioning Environment
https://developer.hashicorp.com/vagrant/tutorials/networking-provisioning-operations

  1. Provision a Virtual Machine
  2. Configure the Network
  3. Explore Other Providers

Comandos de gestión de estados

vagrant init [name [url]]

This initializes the current directory to be a Vagrant environment by creating an initial Vagrantfile if one does not already exist. If a first argument is given, it will prepopulate the config.vm.box setting in the created Vagrantfile. If a second argument is given, it will prepopulate the config.vm.box_url setting in the created Vagrantfile.

vagrant up [name | id]

This is the single most important command in Vagrant, since it is how any Vagrant machine is created.

vagrant halt [name | id]

This command shuts down the running machine Vagrant is managing. Vagrant will first attempt to gracefully shut down the machine by running the guest OS shutdown mechanism. If this fails, or if the --force flag is specified, Vagrant will effectively just shut off power to the machine.

vagrant destroy [name | id]

This command stops the running machine Vagrant is managing and destroys all resources that were created during the machine creation process. After running this command, your computer should be left at a clean state, as if you never created the guest machine in the first place.

vagrant suspend [name | id]

This suspends the guest machine Vagrant is managing, rather than fully shutting it down or destroying it. A suspend effectively saves the exact point-in-time state of the machine, so that when you resume it later, it begins running immediately from that point, rather than doing a full boot. This generally requires extra disk space to store all the contents of the RAM within your guest machine, but the machine no longer consumes the RAM of your host machine or CPU cycles while it is suspended.

vagrant resume [name | id]

This resumes a Vagrant managed machine that was previously suspended, perhaps with the suspend command. The configured provisioners will not run again, by default. You can force the provisioners to re-run by specifying the --provision flag.

vagrant reload [name | id]

The equivalent of running a halt followed by an up. This command is usually required for changes made in the Vagrantfile to take effect. After making any modifications to the Vagrantfile, a reload should be called. The configured provisioners will not run again, by default. You can force the provisioners to re-run by specifying the --provision flag.


Comandos de operación

vagrant ssh [name | id] [-- extra_ssh_args]

This will SSH into a running Vagrant machine and give you access to a shell. On a simple vagrant project, the instance created will be named default. Vagrant will ssh into this instance without the instance name. On multi-machine setups, you can login to each VM using the name as displayed on vagrant status. On a system with machines running from different projects, you could use the id as listed in vagrant global-status.

vagrant global-status --prune

This command will tell you the state of all active Vagrant environments on the system for the currently logged in user.

vagrant status [name | id]

This will tell you the state of the machines Vagrant is managing. It is quite easy, especially once you get comfortable with Vagrant, to forget whether your Vagrant machine is running, suspended, not created, etc. This command tells you the state of the underlying guest machine.

vagrant -h

Get help.

 


Notas personales

 

El index de la documentación oficial se encuentra aquí.

Vagrant se instala como un programa más (.msi). Al instalarlo se añade al PATH la ubicación del binario para poder usar vagrant desde CMD o Git Bash (preferiblemente).

VirtualBox o el provisioner que se quiera usar, debe estar instalado para poder usarlo con Vagrant. Las imagenes de VMs que usa Vagrant no son .iso son .box y se encuentran en Vagrant Cloud (https://app.vagrantup.com/boxes/search). Cada vez que se descarga o se crea mejor dicho un entorno a partir de boxes, los archivos .box se encuentran en C:\Users\<user>\.vagrant.d\boxes para poder se reusados de nuevo rapidamente si se quiere utilizar el mismo tipo de SO.

Ejemplo: creo una maquina CentOS 7 por primera vez; se debe descargar el archivo .box de x GB. Al usar vagrant up para levantar este entorno la primera vez se tarda un poco porque tiene que descargar el archivo, sin embargo al levantar una segunda VM CentOS 7 no tarda casi nada porque ya tiene la box descargada.

Para ver las boxes instaladas:

image.png