Vagrant
## What is Vagrant * System for creation and management of VMs * Wrapper for Hypervisors: VBox, VMware, Docker, AWS, KVM, etc. * Works on Windows, Linux, Mac OS X
## Why Vagrant * provides easy to configure, reproducible, and portable work environments * lowers development environment setup time * increases production parity * consistent interface for usage * Target groups: developers, operators
## About Vagrant * open source * Only CLI * [www.vagrantup.com](https://www.vagrantup.com) * current version: 2.0.2 (2018/01/19) * first release: 2010 * maintained by HashiCorp * VMware plugin is commercial
## All you need is * Vagrant * VirtualBox * Internet connection * a `Vagrantfile`
## Minimal example * Create a Vagrantfile * Startup * Login via SSH * Shutdown * Destroy
## Create a Vagrantfile ```bash $ vagrant init ubuntu/xenial64 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant. ```
## Vagrantfile Minimum viable Vagrantfile ```ruby Vagrant.configure("2") do |config| config.vm.box = "ubuntu/xenial64" # ... plus lots of comments end ```
## Lifecycle ```bash $ vagrant up # downloads and caches the base box # starts and provisions the vagrant environment $ vagrant ssh # connects to machine via SSH # VM is connected via NAT to the network # SSH port of VM is forwarded to port of host $ vagrant halt # stops the vagrant machine $ vagrant destroy # stops and deletes all traces of the vagrant machine ```
## VM customization ```ruby Vagrant.configure("2") do |config| config.vm.provider "virtualbox" do |vb| # Name in VirtualBox GUI vb.name = "elastic" # Customize the amount of memory on the VM vb.memory = "2048" # Customize CPUs vb.cpus = 2 # Enable host's resolver as a DNS proxy in NAT mode vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] end end ```
## Networking ```ruby Vagrant.configure("2") do |config| config.vm.hostname = "elastic" config.vm.network "forwarded_port", guest: 80, host: 80 config.vm.network "private_network", ip: "192.168.56.10" end ```
## Synced folders * VirtualBox shared folders * RSync, SMB, NFS * Folder of Vagrantfile synced to `/vagrant` ```ruby Vagrant.configure("2") do |config| config.vm.synced_folder "src/", "/srv/website" end ```
## Provisioners ```ruby Vagrant.configure("2") do |config| config.vm.provision "m2_settings", type: "file", \ source: "~/.m2/settings.xml", \ destination: "/home/vagrant/.m2/settings.xml" config.vm.provision "script", type: "shell", path: "myscript.sh" end ``` ```bash # Execute provisioners again $ vagrant provision $ vagrant provision --provision-with m2_settings # Start without provisioners $ vagrant up --no-provision ```
## Ansible provisioner * executed from host * executed from VM (ansible_local) * Auto-Generated Inventory ```ruby Vagrant.configure("2") do |config| config.vm.provision "ansible_local" do |ansible| ansible.install = true ansible.verbose = true ansible.playbook = "helloworld.yml" end end ```
## Boxes * Box catalog online * https://app.vagrantup.com/boxes/search * ubuntu/* * centos/* * generic/* * for the advanced: DIY using * Vagrant * Packer
## Managing boxes ```bash $ vagrant box list # Lists all cached boxes $ vagrant box add $BOX_URL # Adds a box from file or URL $ vagrant box update $BOX_NAME # Update box $ vagrant box remove $BOX_NAME # Deletes the base box (but not derived VMs) ```
## Advanced features * Multi-VM environments * Docker swarm * Elasticsearch cluster * Plugins * vagrant-vbguest * vagrant-hostmanager * Customization using ruby code
## Tool support * VSCode Plugins * Vagrant Maven plugin
# The end.