Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created March 22, 2024 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HariSekhon/d753884ef30e6d6e7fa0b22dd6aa1c81 to your computer and use it in GitHub Desktop.
Save HariSekhon/d753884ef30e6d6e7fa0b22dd6aa1c81 to your computer and use it in GitHub Desktop.
vagrant.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowlege-Base

Vagrant

https://www.vagrantup.com/

Written in Ruby with a declarative Vagrantfile to declare the VMs, their settings, networking and provisioning scripts for automation of the VM contents.

Useful to creating and maintaining local clusters of VMs for reproducing client's problems on specific versions of software. Used this at Cloudera a lot.

Install

Install Doc

Downloads

Vagrantfile Templates

HariSekhon/Vagrant-templates

HariSekhon/Templates - Vagrantfile

Official Boxes

https://app.vagrantup.com/boxes/search

Vagrant CLI

vagrant box list

Create your Vagrantfile from the templates above with the VMs, settings and provisioner scripts you want.

cd somedir/  # which contains a Vagrantfile

Boots the VM(s) specified in the Vagrantfile from the base box image(s), configures it with the settings and runs the provisioning scripts / Puppet / Ansible etc:

vagrant up

SSH into the VM - auto-determines the IP address from VirtualBox:

vagrant ssh # <vm_name>

Inside VM see mounted shared folder of content shared with your host desktop / laptop:

cd /vagrant
ls -l
vagrant down

Delete the VM:

vagrant destroy

Environment Variables

Override the default Vagrantfile:

VAGRANT_VAGRANTFILE=Vagrantfile-centos vagrant up
VAGRANT_VAGRANTFILE=Vagrantfile-centos vagrant ssh

More logging:

VAGRANT_LOG=INFO vagrant ....

Shared Folders

VBoxAdditions are required for shared folders

Uses VirtualBox commands to set up machine.

It prefixes VBoxManage commands with sudo - fails if require_tty is set in /etc/sudoers.

Networking is done via SSH commands, just make sure to not require_tty in /etc/sudoers.

Problem nanifests as:

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

touch /etc/sysconfig/network-scripts/ifcfg-eth1

Fix:

sudo perl -pi -e 's/^(\s*Defaults\s+requiretty)/#$1/' /etc/sudoers

Packaging

  • install vbox additions (see virtualbox)
  • remove extra network interfaces for internal network since vagrant will recreate them
  • remove extra USB, Audio ports

On a Linux VM to have it self-resolve its own FQDN properly:

cat >> /etc/rc.local:
ip=$(ifconfig | grep -A1 eth1 | grep "inet addr" | sed 's/^.*addr://;s/[[:space:]].*$//')
if [ -n "$ip" ] && ! grep -q "^$ip " /etc/hosts;then
    echo "$ip $HOSTNAME.local $HOSTNAME" >> /etc/hosts
fi
sed -i "s/^127.0.0.1.*/127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localhost/" /etc/hosts

Build a base box from the normal VM, vagrant/repkg.sh for convenience:

vagrant package --base centos6-base --output centos6.box &&
vagrant box remove centos6 &&
vagrant box add centos6 centos6.box &&
mv centos6.box ../boxes/

Mount /vagrant manually if it doesn't auto-mount:

sudo mount -t vboxsf v-root /vagrant/
Ported from private Knowledge Base page 2013+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment