Skip to content

Instantly share code, notes, and snippets.

@mjuenema
Last active December 5, 2016 08:05
Show Gist options
  • Save mjuenema/08b99027e74ec771d8f65787ef85ed6d to your computer and use it in GitHub Desktop.
Save mjuenema/08b99027e74ec771d8f65787ef85ed6d to your computer and use it in GitHub Desktop.

Vagrant Notes

Installing and customising Vagrant

Install Vagrant from the download page to ensure the most recent version is installed.

Add some useful add-ons.

$ vagrant plugin install vagrant-vbguest                    # automatic installation of VB Guest Additions.
$ vagrant plugin install vagrant-cachier                    # cache yum/apt packages of identical boxes
$ vagrant plugin install vagrant-persistent-storage         # persistent storage for a box.
$ vagrant plugin install vagrant-remove-old-box-versions    #
$ vagrant plugin install vagrant-proxyconf

Virtualenv for Ansible provisioning

$ workon Ansible || mkvirtualenv Ansible
(Ansible)$ pip install --upgrade ansible

First time creation of VM

(Ansible)$ mkdir shared
(Ansible)$ vim Vagrantfile
(Ansible)$ vim provision.yml
(Ansible)$ vim requirements.yml

(Ansible)$ vagrant box update
(Ansible)$ vagrant up [--provider=virtualbox]

Packaging an existing VM

This is useful to speed up provisioning since that step will have run already.

$ vagrant package --output centos7postinstall.box

Description of add-ons

#!/bin/sh
#
# (Pre-)Provision a VM by installing the `ansible-playbooks.git` repository
# and dependencies. This allows to run the Ansible provisioner from within
# the VM.
#
yum -y install git epel-release libffi-devel libyaml-devel openssl-devel
yum -y install python-virtualenvwrapper python-pip ansible
sudo -u vagrant -i git clone --recursive https://github.com/mjuenema/ansible-playbooks.git
---
# provision.yml
- hosts: all
become: no
become_user: root
vars:
# testing must be set to true to prevent Ansible needing the vault password!
testing: false
ssh_role: true
ntp_role: true
dev_role: false
pydev_role: false
ansible_role: false
roles:
- { role: mjuenema.postinstall }
- src: https://github.com/mjuenema/ansible-role-postinstall.git
name: mjuenema.postinstall
version: master
# -*- mode: ruby -*-
# vi: set ft=ruby :
NAME = "testpostinstall"
BOX = "centos/7"
MEMORY = 512
CPU = 1
Vagrant.configure("2") do |config|
config.vm.box = BOX
config.vm.hostname = NAME
config.vm.synced_folder ".", "/home/vagrant/shared"
config.vm.provider "virtualbox" do |vb|
vb.name = NAME
vb.gui = false
vb.memory = MEMORY
vb.cpus = CPU
vb.linked_clone = true
end
config.vm.provision "shell", path: "provision.sh"
config.vm.provision "ansible" do |ansible|
ansible.ask_vault_pass = true
ansible.verbose = "vvvv"
ansible.playbook = "provision.yml"
ansible.galaxy_role_file = "requirements.yml"
end
if Vagrant.has_plugin?("vagrant-cachier")
# Configure cached packages to be shared between instances of the same base box.
# More info on http://fgrehm.viewdocs.io/vagrant-cachier/usage
config.cache.scope = :box
end
if Vagrant.has_plugin?("vagrant-persistent-storage")
config.persistent_storage.enabled = true
config.persistent_storage.location = "data.vdi"
config.persistent_storage.size = 1000
config.persistent_storage.mountname = 'data'
config.persistent_storage.filesystem = 'ext4'
config.persistent_storage.mountpoint = '/data'
config.persistent_storage.volgroupname = 'datavol'
end
# TODO vagrant-proxycon
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment