HashiCorp/Vagrant
Introduction
Vagrant is configured on a per project basis. Each of these projects has its own Vagran file. The Vagrant file is a text file in which vagrant reads that sets up our environment. There is a description of what OS, how much RAM, and what software to be installed etc. You can version control this file.
Ruby gotchas
Vagrant configuration file is written in Ruby specification, therefore you need to remember
- don't use dashes in object names, don't: jenkins-minion_config.vm.box = "ubuntu/xenial64"
- don't use symbols (here underscore) in variable names, don't: (1..2).each do |minion_number|
Install
Check your distro most likely Ubuntu version candidate
apt-cache vagrant
Download from https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb
wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.debb sudo dpkg -i vagrant_1.9.5_x86_64.deb sudo apt-get update && sudo apt-get install -f #to resolve missing dependencies if any
Install ruby is recommended as configuration within Vagrant file is written in Ruby language.
sudo apt-get install ruby sudo gem install bundler sudo gem update bundler #if update needed
Version upgrade
In this example I was successful to update version 1.9.5 to 2.1.1 on top of old package.#
- Download new version
- Install using dpkg -i
- If you see errors you can attempt to try
vagrant plugin repair #I used vagrant plugin expunge --reinstall vagrant plugin update #Then updated plugin 'vagrant-proxyconf'
Images management
So, you know Vagrant comes with a preconfigured image repositories.
- Add boxes
These are standard VMs from providers in Virtualbox, VMware or Hyper-V format taken from a given repository
vagrant box add hashicorp/precise64 #user: hashicorp boximage: precise64, this is preconfigured repository vagrant box add ubuntu/xenial64 vagrant box add ubuntu/xenial64 --box-version 20170618.0.0 --provider virtualbox
Box can be specified via URLs or local file paths, Virtualbox can only nest 32bit machines
vagrant box add --force ubuntu/14.04 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
vagrant box add --force ubuntu/14.04-i386 https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box
- Manage boxes using
vagrant box list | add | remove
vagrant init - your first project
- Configure Vagrantfile to use the box as your base system
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" config.vm.hostname = "ubuntu" #hostname, requires reload end
- Create Vagrant project, by creating Vagrantfile in your current directory
vagrant init #initialises an project vagrant init ubuntu/xenial64 # initialises official Ubuntu 16.04 LTS (Xenial Xerus) Daily Build vagrant init ubuntu/bionic64 #supports only Virtualbox provider vagrant init bento/ubuntu-18.04 #supports variety of providers
- Power up your Vagrant box
vagrant up Error: Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time period. ...
The error above is due to Virtualbox cannot run nested 64bit virtualbox VM. Spinning up a 64bit VM stops with an error that no 64bit CPU could be found.
- Ssh to the box
piotr@vm-ubuntu64:~/git/vagrant$ vagrant ssh #default password is "vagrant" vagrant@vagrant-ubuntu-precise-32:~$ w 13:08:35 up 15 min, 1 user, load average: 0.06, 0.31, 0.54 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT vagrant pts/0 10.0.2.2 13:02 1.00s 4.63s 0.09s w
- Shared directory between Vagrant VM and an hypervisor provider
Vagrant VM shares a directory mounted at /vagrant with the directory on the host containing your Vagrantfile. This can be manually mounted from within VM as long the shared directory is setup in GUI.
Eg. vm_name > Settings > Shared Folders > Name: vagrant | Path: /home/piotr/vm_name
sudo mount -t vboxsf -o uid=1000 vagrant /vagrant #firts arg 'vagrant' refers to GUI setiing
Troubleshooting
vagrant --debug up
Manage power states
vagrant suspend
- saves the current running state of the machine and stop itvagrant halt
- gracefully shuts down the guest operating system and power down the guest machinevagrant destroy
- removes all traces of the guest machine from your system. It'll stop the guest machine, power it down, and remove all of the guest hard disks
Lookup path precedence for Vagrant project file
When you run any vagrant command, Vagrant climbs your directory tree starting first in the current directory you are in. Example:
/home/peter/projects/la/Vagrant /home/peter/projects/Vagrant /home/peter/Vagrant /home/Vagrant /Vagrant
Configuration
Networking
Private network is network that is not accessible from Internet. Networking stanza is a part of the main |config| loop.
DHCP IP address assigned
config.vm.network "private_network", type: "dhcp"
Static IP assigment
config.vm.network "private_network", ip: "192.168.80.5" auto_config: false #optional to disable auto-configure
Public network These networks can be accessible from outside of the host machine including Internet, are usually Bridged Networks.
Examples of dhcp and static IP assignment
config.vm.network "public_network", type: "dhcp" config.vm.network "public_network", ip: "192.168.80.5"
Default interface. The name need to match your system name otherwise Vagrant will prompt you to choose from available interfaces during vagrant up process.
config.vm.network "public_network", bridge: 'eth1'
Port forwarding
Vagrant can forward any host(hypervisor) TCP port to guest vm specyfing in ~/git/vargant/Vagrant file
config.vm.network :forwarded_port, guest: 80, host: 4567
Reload virtual machine vagrant reload
and run from hypervisor web browser http://127.0.0.1:4567 to test it.
Sync folders
First folder is on a host machine and the second is mapped on Vagrant guest vm.
Disabling
Vagrant.configure("2") do |config| config.vm.sync_folder "../data/", "/vagrant-data", disabled: true end
Modifying the Owner/Group
config.vm.sync_folder "../data/", "/vagrant-data", disabled: true, owner: "root", group: "root"
Vagrant providers
Vagrant can work with a wide variety of backend providers, such as VMware, AWS, and more without changing Vagrantfile. It's enough to specify the provider and Vagrant will do the rest:
vagrant up --provider=vmware_fusion vagrant up --provider=aws
Provisioners
Shell provisioner
Vagrant can run from shared location script or from inline: Vagrant file shell provisioning commands.
Create provisioning script
vi ~/git/vagrant/bootstrap.sh #!/usr/bin/env bash export http_proxy=<nowiki>http://username:password@proxyserver.local:8080</nowiki> export https_proxy=$http_proxy apt-get update apt-get install -y apache2 if ! [ -L /var/www ]; then rm -rf /var/www ln -sf /vagrant /var/www # sets Vagrant shared dir to Apache DocumentRoot fi
Configure Vagrant to run this shell script above when setting up our machine
vi ~/git/vagrant/Vagrantfile Vagrant.configure("2") do |config| config.vm.box = ubuntu/14.04-i386 #the provision line tells Vagrant to use the shell provisioner to setup the machine config.vm.provision: shell, path: "bootstrap.sh" end
Bring the environment up
vagrant up #runs provisioning only once vagrant reload --provision #reloads VM skipping import and runs provisioning vagrant ssh #ssh to VM wget -qO- 127.0.0.1 #test Apache is running on VM
- Provisioners - shell, ansible, ansible_local and more
This section is about using Ansible with Vagrant,
*ansible
, where Ansible is executed on the Vagrant host *ansible_local
, where Ansible is executed on the Vagrant guest
Ansible provisioner
Specify Ansible as a provisioner in Vagrant file
# # Run Ansible from the Vagrant Host # config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end
Chef_solo provisioner
Create recipe, the following dirctory structure is required, eg. recipe name is: vagrant_la
├── cookbooks │ └── vagrant_la │ └── recipes │ └── default.rb Vagrant
vi cookbooks/vagrant_la/recipes/default.rb execute "apt-get update" package "apache2" execute "rm -rf /var/www" link "var/www" do to "/vagrant" end
In Vagrant file add following
config.vm.provision "chef_solo" do |chef| chef.add_recipe "vagrant_la" end
Run vagrant up
Puppet manifest
Create Vagrant provisioning stanza
config.vm.define "web" do |web| web.vm.hostname = "web" web.vm.box = "apache" web.vm.network "private_network", type: "dhcp" web.vm.network "forwarded_port", guest: 80, host: 8080 web.vm.provision "puppet" do |puppet| puppet.manifests_path = "manifests" puppet.manifest_file = "default.pp" end end
Create a required folder structure for puppet manifests
├── manifests │ └── default.pp └── Vagrantfile
Puppet manifest file
vi manifests/default.pp exec { "apt-get update": command => "/usr/bin/apt-get update", } package { "apache2": require => Exec["apt-get update"], } file { "/var/www": ensure => link, target => "/vagrant", force => true, }
Box images advanced
vagrant box list #list all downloaded boxes
Default path of boxes image, it can be specified by environment variable VAGRANT_HOME
C:\Users\%username%\.vagrant.d\boxes #Windows ~/.vagrant.d/boxes #Linux
Change default path via environment variable
export VAGRANT_HOME=my/new/path/goes/here/
Box format
When you un-tar the .box file it contains 4 files:
|--Vagrantfile |--box-disk1.vmdk #compressed virtual disk |--box.ovf #description of virtual hardware |--metadata.json
Create box from current project (package box)
This allows to create a reusable box that contains all changes to the software we made
$ vagrant package web ==> web: Attempting graceful shutdown of VM... ==> web: Clearing any previously set forwarded ports... ==> web: Exporting VM... ==> web: Compressing package to: /home/piotr/vagrant/package.box $ vagrant box add web package.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'web' (v0) for provider: box: Unpacking necessary files from: file:///home/piotr/vagrant/package.box
After this process you will find the box in ~/.vagrant.d/boxes location and you can also list them using vagrant box list.
Enable Vagrant to use proxy server for VMs
Install proxyconf plugin or use vagrant plugin list
to verify if installed
vagrant plugin install vagrant-proxyconf
Configure your Vagrantfile, here particularly host 10.0.0.1:3128 runs CNTLM proxy
Vagrant.configure("2") do |config| config.proxy.http = "http://10.0.0.1:3128" config.proxy.https = "http://10.0.0.1:3128" config.proxy.no_proxy = "localhost,127.0.0.1"
Virtualbox Guest Additions - upgrade
Automatic sync using plugin
$ vagrant gem install vagrant-vbguest #for vagrant < 1.1.5 $ vagrant plugin install vagrant-vbguest #for vagrant 1.1.5+
Verify current version
vagrant vbguest --status
For Vagrant 1.1.5+, add to your Vagrant file, to
config.vbguest.auto_update = false config.vbguest.no_remote = true
Then install the correct version matching your host installation
vagrant vbguest --do install
Full command options
$ vagrant vbguest [vm-name] [--do start|rebuild|install] [--status] [-f|--force] \ [-b|--auto-reboot] [-R|--no-remote] [--iso VBoxGuestAdditions.iso] [--no-cleanup]
More you will find at vagrant-vbguest plugin project.
Manual upgrade
Find out what version you run
$ modinfo vboxguest
or
$ lsmod | grep -io vboxguest | xargs modinfo | grep -iw version version: 4.3.36_Ubuntu
or
$ sudo /usr/sbin/VBoxService --version 4.3.36_Ubuntur105129
Download the extension, you can explore here
wget http://download.virtualbox.org/virtualbox/5.0.32/VBoxGuestAdditions_5.0.32.iso
wip... see references for the rest of steps, next step instal vagrant plugin vb...
References
List all Virtualbox SSH redirections
vboxmanage list vms | cut -d ' ' -f 2 > /tmp/vms.out && for vm in $(cat /tmp/vms.out); do vboxmanage showvminfo "$vm" | grep ssh; done vboxmanage list vms | cut -d ' ' -f 1 | sed 's/"//g' > /tmp/vms.out && for vm in $(cat /tmp/vms.out); do echo $vm; vboxmanage showvminfo "$vm" | grep ssh; done vboxmanage list vms \ | cut -d ' ' -f 1 \ | sed 's/"//g' > /tmp/vms.out \ && for vm in $(cat /tmp/vms.out); do vboxmanage showvminfo "$vm" \ | grep ssh \ | tr --delete '\n'; echo " $vm"; done sed 's/"//g' #removes double quotes from whole string tr --delete '\n' #deletes EOL, so the next command output is appended to the previous line
HAProxy cluster, multi-node Vagrant config
git clone https://github.com/jweissig/episode-45
This creates Ansible mgmt server, Load Balancer and Web nodes [1..2]. HAProxy will be configured via Ansible code.
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| # create mgmt node config.vm.define :mgmt do |mgmt_config| mgmt_config.vm.box = "ubuntu/trusty64" mgmt_config.vm.hostname = "mgmt" mgmt_config.vm.network :private_network, ip: "10.0.15.10" mgmt_config.vm.provider "virtualbox" do |vb| vb.memory = "256" end mgmt_config.vm.provision :shell, path: "bootstrap-mgmt.sh" end # create load balancer config.vm.define :lb do |lb_config| lb_config.vm.box = "ubuntu/trusty64" lb_config.vm.hostname = "lb" lb_config.vm.network :private_network, ip: "10.0.15.11" lb_config.vm.network "forwarded_port", guest: 80, host: 8080 lb_config.vm.provider "virtualbox" do |vb| vb.memory = "256" end end # create some web servers # https://docs.vagrantup.com/v2/vagrantfile/tips.html (1..2).each do |i| config.vm.define "web#{i}" do |node| node.vm.box = "ubuntu/trusty64" node.vm.hostname = "web#{i}" node.vm.network :private_network, ip: "10.0.15.2#{i}" node.vm.network "forwarded_port", guest: 80, host: "808#{i}" node.vm.provider "virtualbox" do |vb| vb.memory = "256" end end end end
Boot strap script bootstrap-mgmt.sh
#!/usr/bin/env bash # install ansible (http://docs.ansible.com/intro_installation.html) apt-get -y install software-properties-common apt-add-repository -y ppa:ansible/ansible apt-get update apt-get -y install ansible # copy examples into /home/vagrant (from inside the mgmt node) cp -a /vagrant/examples/* /home/vagrant chown -R vagrant:vagrant /home/vagrant # configure hosts file for our internal network defined by Vagrantfile cat >> /etc/hosts <<EOL # vagrant environment nodes 10.0.15.10 mgmt 10.0.15.11 lb 10.0.15.21 web1 10.0.15.22 web2 10.0.15.23 web3 10.0.15.24 web4 10.0.15.25 web5 10.0.15.26 web6 10.0.15.27 web7 10.0.15.28 web8 10.0.15.29 web9 EOL
Gitbash path - /c/Program\ Files/Oracle/VirtualBox/VBoxManage.exe
Set bootstrap script for Proxy or No-proxy specific system
Vagrant status Vagrant up Vagrant ssh mgmt ansible all --list-hosts ssh-keyscan web1 web2 lb > ~/.ssh/known_hosts ansible-playbook ssh-addkey.yml -u vagrant --ask-pass ansible-playbook site.yml
Once set it up you can navigate on your laptop to:
http://localhost:8080/ #Website test http://localhost:8080/haproxy?stats #HAProxy stats
Use to verify end server
curl -I http://localhost:8080
Generate web traffic
vagrant ssh lb sudo apt-get install apache2-utils ansible localhost -m apt -a "pkg=apache2-utils state=present" --become ab -n 1000 -c 1 http://10.0.2.15:80/
Enable mDNS discovery
Install avahi on all machines a system which facilitates service discovery on a local network via the mDNS/DNS-SD protocol suite
config.vm.provision "shell", inline: <<-SHELL apt-get install -y avahi-daemon libnss-mdns SHELL
Resources
- nss-mdns system which allows hostname lookup of *.local hostnames via mDNS in all system programs using nsswitch
- avahi.org
Ubuntu with GUI
This article is going to describe to setup Vagrant Virtualbox VM with GUI, setting up xserver with xfce4 as desktop environment.
Locales
This is not working
locale-gen en_GB.utf8 #en_GB.UTF-8 update-locale LANG=en_GB.UTF-8 locale-gen --purge "en_GB.UTF-8" dpkg-reconfigure --frontend=noninteractive locales dpkg-reconfigure --frontend=noninteractive keyboard-configuration localedef -i en_GB -c -f UTF-8 en_GB.utf8 sudo update-locale LANG=en_GB.UTF-8 locale-gen --purge "en_GB.UTF-8"
Gnome3
This setup installs Ubuntu desktop and may require a restart to apply changes to like a taskbar with shortcuts.
# -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" #bento/ubuntu-18.04, ubuntu/xenial64 config.vm.hostname = "u18gui-1" # Manually check for updates `vagrant box outdated` config.vm.box_check_update = false # Vbguest plugin config.vbguest.auto_update = false config.vbguest.no_remote = true # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" # Public network, which generally matched to bridged network. # config.vm.network "public_network" # config.vm.synced_folder "hostDir", "/InVagrantMount/path" # config.vm.synced_folder "../data", "/vagrant_data" config.vm.provider "virtualbox" do |vb| vb.gui = true vb.memory = "3072" vb.name = "u18gui-1_vagrant" end config.vm.provision "shell", inline: <<-SHELL export DEBIAN_FRONTEND=noninteractive setxkbmap gb apt-get update && apt-get upgrade -y apt-get install -y ubuntu-desktop --no-install-recommends apt-get install -y terminator tmux #only U16 xenial to fix Unity #apt-get install -y unity-lens-files unity-lens-applications indicator-session --no-install-recommends SHELL end
Running up
vagrant plugin install vagrant-vbguest vagrant up && vagrant vbguest --do install && vagrant reload
Xface
Get a basic Ubuntu image working, boot it up and vagrant ssh. Next, enable the VirtualBox display, which is off by default. Halt the VM and uncomment these lines in Vagrantfile:
config.vm.provider :virtualbox do |vb| vb.gui = true end
Boot the VM and observe the new display window. Now you just need to install and start xfce4. Use vagrant ssh and:
sudo apt-get install -y xfce4 virtualbox-guest-dkms virtualbox-guest-utils virtualbox-guest-x11 #guest additions are already installed on most of the Vagrant boxes
Don't start the GUI as root because you really want to stay the vagrant user. To do this you need to permit anyone to start the GUI:
sudo vim /etc/X11/Xwrapper.config and edit it to allowed_users=anybody sudo startxfce4& sudo VBoxClient-all #optional
You should be landed in a xfce4 session.
(Optional) If VBoxClient-all script isn't installed or anything is missing, you can replace with the equivalent:
sudo VBoxClient --clipboard sudo VBoxClient --draganddrop sudo VBoxClient --display sudo VBoxClient --checkhostversion sudo VBoxClient --seamless
References
- Vagrant GUI vms stackoverflow
WIP DevOps workstation
This to contain:
- bashrc with git branch in ps1
- bash autocomplete (...samename)
- bash colored symlinks
- bash_logout and .profile to eval ssh-agent and kill on exit
- git install
- ansible 1.9.4
- java Oracle
- clone tfenv and install terraform
- vim install
- vundle install
- [done] python 2.7 OOB in 16.04
- [done]python pip: awscli, boto, boto3, etc..
Challenges:
- Ubuntu 16.04 official box does not come with a default vagrant user but instead comes with ubuntu user. This causes a number of incompatibilities.
- Read more at launchpad vagrant xenial box is not provided with vagrant/vagrant username and password
- Solutions
- on W10 host both users: ubuntu & vagrant exist. Only vagrant has insecure_pub installed OOB. I am coping vagrant user pub key into ubuntu user authorized_keys
- on U16.05 host the official image does not seem to come with vagrant user but Ubuntu user works OOB
- Read more at SO
- [Vagrant's Ubuntu 16.04 vagrantfile default password https://stackoverflow.com/questions/41337802/vagrants-ubuntu-16-04-vagrantfile-default-password]
- How do I add my own public key to Vagrant VM?
- Running a Shell Script Once Only in vagrant
References
- Vagrant Start up documentation
- Vagrant Hashicorp VMs repository Virtualbox
- Vagrant Ubuntu VMs images Virtualbox
- Vagrant and Ansible provisioner Vagrant docs