Vagrant

From XennisWiki
Jump to: navigation, search

Vagrant is computer software that creates and configures virtual development environments. It can be seen as a higher-level wrapper around virtualization software such as VirtualBox, VMware, KVM and Linux Containers (LXC), and around configuration management software such as Ansible, Chef, Salt, and Puppet. (Wikipedia, Vagrant)

Installation

Install VirtualBox

apt-get install virtualbox virtualbox-dkms

Install Vagrant

apt-get install vagrant

Basic usage

Install a virtual machine with Ubuntu 12.04 (precise) 64-bit

vagrant init hashicorp/precise64
vagrant up

SSH to the virtual machine

vagrant ssh

(1) Reboot the VM, (2) shut it down or (3) destroy it

vagrant reload
vagrant halt
vagrant destroy

Usage with Ansible

Further information: Ansible Provisioner, Using Vagrant and Ansible

Configuration Vagrant

Create a configuration file named Vagrantfile

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/xenial64"

  config.vm.network :private_network, ip: "192.168.33.15"

  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--name", "ExampleApp", "--memory", "1024"]
  end

  # Shared folder from the host machine to the guest machine. Uncomment the line
  # below to enable it.
  #config.vm.synced_folder "../../../example-app", "/webapps/exampleapp/example-app"

  # Ansible provisioner.
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "vagrant.yml"
    #ansible.host_key_checking = false
    #ansible.verbose = "v"
  end
end

Configure Ansible

According to the specified ansible.playbook create a playbook vagrant.yml

---

- name: Create a {{ application_name }} virtual machine via vagrant
  hosts: all
  sudo: yes
  sudo_user: root
  remote_user: vagrant
  vars:
    - update_apt_cache: yes
  vars_files:
    - env_vars/base.yml
    - env_vars/vagrant.yml

  roles:
    - base
    - web

Run

Run the Vagrant script to setup a VirtualBox and run the Ansible playbook

vagrant up

Open http://192.168.33.15

To re-run the Ansible playbook on the existing VM use: vagrant provision

See also

External links