Vagrant

Basic information

Installation

You can find the Vagrant’s latest version in the link https://www.vagrantup.com/ downloads.html . Also, you can install it with your package manager (check the version before install it).

Usage

Vagrant commands usually have the following structure: vagrant command [<args>] but in this particular case, we have custom options that help with our ansible configuration so the structure changes to: vagrant --custom-option=option -- command [<args>]

Note

  • Be careful with the spaces between the double minus and the command.
  • If you use a diferent configuration for vagrant remember to delete the custom option --custom-option=option -- to use the vagrant’s usual commands
  1. Use the up command alongside the --provision argument to import the VM into VirtualBox and run ansible.

    vagrant --machine=<single> -- up --provision
    
  2. Check if ansible ran successfully. If it didn’t, find the error and patch it.

  3. Test your patch by syncing the changes and re-provision.

    vagrant --machine=<single> -- rsync
    vagrant --machine=<single> -- provision
    

Note

Our Vagrant’s custom options are:

  • machine: Machine hostname for ansible and the playbooks’s name.
  • vault-id: The password file for ansible.

Useful commands

  • ssh: Connects to machine via SSH. vagrant ssh
  • reload: Restarts vagrant machine, loads new Vagrantfile configuration. vagrant reload
  • halt: Stops the vagrant machine. vagrant halt
  • destroy: Stops and deletes all traces of the vagrant machine. vagrant destroy

For more help run vagrant -h or vagrant <command> -h for help on any individual command.

Vagrantfile

The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines [1] . The syntax of Vagrantfile is Ruby.

This vagrantfile is based on a vagranfile with custom variables to ease the use of ansible_local handling and other options.

# -*- mode: ruby -*-                                                         
#          
# Title       : vagrantfile                                                  
# Description : A vagranfile example with some custom configuration.
# Author      : Manuela Carrasco
# Date        : Jan 21, 2019
# Usage       : vagrant [options] -- command [<args>] 
# Help        : vagrant -h                                                   
#==============================================================================

# This example has custom options like "machine" but we won't show the code
# where the extra options were added.

Vagrant.configure("2") do |config| # The vagrantfile api version is 2
  # The virtual machine image. For more information
  # https://www.vagrantup.com/docs/vagrant-cloud/
  config.vm.box = "centos/7"
  # Custom configuration for virtualbox machine
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id,
                  "--memory", "2048"]
  end
  # Configure ansible in the VM
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "./site.yml"
    # Perform the playbook tasks as another user
    ansible.become = true
    # machine is a custom option for the hostname
    # and it's the playbooks name
    ansible.groups = {
      "#{machine}" => ["default"],
      "all:children" => ["#{machine}"]
    }
    # vault_id is a password file for ansible
    if not vault_id.empty?
      ansible.vault_password_file = "#{vault_id}"
    end
  end
  config.vm.hostname = "my-virtualmachine" # Set VM hostname
      

Note

For more information about vagrantfile and how to create it go to https://www.vagrantup.com/docs/vagrantfile/

Troublehooting

Author:

References

[1]Vagrantfile. (n.d.). Retrieved January 21, 2019, from https://www.vagrantup.com/docs/vagrantfile/