Wednesday 4 June 2014

Setting up an ubuntu vagrant instance

Install vagrant

Download the latest deb from http://www.vagrantup.com/downloads.html

Install vagrant from downloaded deb

$ sudo dpkg -i ./vagrant.deb

Install virtualbox

Add the appropriate deb source to your apt sources

$ echo "deb http://download.virtualbox.org/virtualbox/debian trusty contrib" | sudo tee -a /etc/apt/sources.list

Add the oracle public key

$ wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -

Update the apt cache

$ sudo apt-get update

Install virtualbox

$ sudo apt-get install virtualbox-4.3

Initialise a vagrant instance

Note that if you're loading a 64 bit vm you need to have hardware virtualisation enabled in your bios (and your processor needs to support it!)

$ vagrant init ubuntu/trusty64
$ vagrant up

ssh into vm

$ vagrant ssh

halt/suspend/destroy vm

$ vagrant suspend saves current state of vm and stops it - fast to resume, uses more space
$ vagrant halt    saves current state of vm and shuts down - slower to resume, less space
$ vagrant destroy destroys all traces of the vm - no space used

Enable remote ssh access

By default vagrant will only create a private network between the host and vm. By changing to a public network, the vm will be allocated an ip address from your LAN and you will be able to ssh in from a remote machine

In the Vagranfile:

config.vm.network "public_network", bridge: 'eth0'

Reload Vagrantfile:

$ vagrant reload

You can ssh into the vm (vagrant ssh) and find out the ip address (ifconfig), allowing you to now ssh directly into the machine

$ ssh vagrant@192.168.1.xxx

Enable provisioning of the vm with ansible

Requires ansible to be installed
In the Vagrantfile

config.vm.provision :ansible do |ansible|
    ansible.playbook = "ansible/provision.yml"
    ansible.inventory_path = "ansible/hosts"
    ansible.limit = "all"
end


Create a file called ansible/hosts which has the vagrant vm listed in it

[vagrant]
192.168.1.xxx


Create a file called ansible/provision.yml which will be our playbook

---
- hosts: vagrant
tasks:
    - name: test vm is up
      ping:


Provision the vm
 
$ vagrant provision

No comments:

Post a Comment