Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a VAGRANT cheat sheet

Vagrant Cheat Sheet

add image

local

$ vagrant box add {title} {url}
$ vagrant init {title}
$ vagrant up

public (publicly available catalog of Vagrant boxes)

$ vagrant box add ubuntu/precise64
$ vagrant init ubuntu/precise64
$ vagrant up

connect

$ vagrant ssh {name}

remove @ virtualbox level

$ vagrant destroy

remove @ vagrant level

$ vagrant box remove {title}

list boxes

$ vagrant box list

provisioning

$ vagrant provision

export vagrant image

run script to compress Ubuntu: https://gist.github.com/carlessanagustin/2fb92e88f2068300a2ed

$ vagrant package --output package.box

2. Vagrantfile cheat sheet

port forwarding

# host:port >> guest:port
zipi.vm.network "forwarded_port", host: 8080, guest: 80, auto_correct: true

mount folders and permissions

zipi.vm.synced_folder ".", "/vagrant",
  owner: "vagrant",
  group: "vagrant",
  mount_options: ["dmode=775,fmode=664"]

network

zipi.vm.network "private_network",
    ip: "192.168.32.10",
    virtualbox__intnet: true,
    auto_config: true

provider

config.vm.provider "virtualbox" do |vb|
    vb.memory = 512
    vb.cpus = 1
    #vb.gui = true
end

provisioning: inline

  • Option 1:
config.vm.provision "shell", inline: "apt-get update && apt-get -y upgrade"
  • Option 2:
$script = <<SCRIPT
apt-get update
apt-get -y upgrade
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: $script
end

provisioning: shell

config.vm.provision :shell, :path => "vagrant-bootstrap.sh"

provisioning: ansible

config.vm.provision "ansible" do |ansible|
  ansible.playbook = "provision-ansible/install.yml"
  
  ansible.verbose = 'vvvv'
  
  ansible.host_key_checking = false
  ansible.sudo = true
    
  ansible.tags = ['base', 'ansible']
  #ansible.skip_tags = ''

  # STATIC INVENTORY
  #ansible.inventory_path = "provision-ansible/hosts/all"
  #ansible.limit = 'vagrant'
    
  # AUTO-GENERATED INVENTORY
  ansible.groups = {
    "group1" => ["zipi"],
    "all_groups:children" => ["group1"],
    #"group1:vars" => { "vagrant_enable" => true }
  }

  ansible.extra_vars = {
      ansible_ssh_user: 'vagrant',
      vagrant_enable: true
      }
end

launching 2 machines

config.vm.define "zipi" do |zipi|
    zipi.vm.host_name = "zipi"
    zipi.vm.box = "ubuntu/trusty64"
    zipi.vm.network "private_network", ip: "192.168.32.10", virtualbox__intnet:     true, auto_config: true
end
config.vm.define "zape" do |zape|
    zape.vm.host_name = "zape"
    zape.vm.box = "ubuntu/trusty64"
    zape.vm.network "private_network", ip: "192.168.32.11", virtualbox__intnet:     true, auto_config: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment