This is an old revision of the document!
Table of Contents
Vagrant
By Hashicorp.
WOrkflow
Overview commands
box - manage "boxes" destroy - shut down the VM then delete its stored image? gem halt - shut down the VM init - prepare a directory with a new Vagrantfile package - shut down the VM, then convert it to a 'package' which can be turned into a box? (Or something) provision - run just the provisioning (eg, Chef, Puppet...) stage reload - modify the VM configuration (eg, reapply Vagrantfile), reboot the VM, reprovision the modified parts of the vagrantfile resume - un-suspend (ie, unhibernate) ssh - open an SSH shell connection to the VM ssh-config status suspend - hibernate the VM up - some or all of: copy a VM image to create a new VM, apply configuration to it, boot it
Vagrantfile
The variable from outside the provisioning block can be accessed as
"#{PATH_VAGRANT}"
The ENvironment Varialbe from the host can be accessed as. This is the way to pass infos to the vagrantfile from the host system. Into the provisioner it then may be passed in as a vagrant variable
GIT_USERNAME=ENV['GIT_USERNAME'] # Use shell script to provision config.vm.provision "shell", inline: <<-SHELL echo "#{PATH_VAGRANT}" SHELL
Getting the path to the folder of the vagrantfile e.g. in order to copy files from the relative location to the vm.
PATH_VAGRANT=File.dirname(__FILE__) # Use shell script to provision config.vm.provision "shell", inline: <<-SHELL # echo the full path echo -e "Source: #{PATH_VAGRANT}\.aws\config" SHELL
Copying files is done with an own provisioner
PATH_VAGRANT=File.dirname(__FILE__) # upload the AWS configs config.vm.provision "file", source: "#{PATH_VAGRANT}\\.aws\\config", destination: "~/.aws/config" config.vm.provision "file", source: "#{PATH_VAGRANT}\\.aws\\credentials", destination: "~/.aws/credentials" config.vm.provision "file", source: "#{PATH_VAGRANT}\\.aws\\answerfile", destination: "~/.aws/answerfile"
Escaping
The escapes, e.g. echo of quotes - must be escaped double times :
\\"
docker exec tomcat8 bash -c "echo -e '<role rolename=\\"manager-gui\\"/>' >> /usr/local/tomcat/conf/tomcat-users.xml"
Box
Howto create a new box
# list vms vboxmanage list vms # make sure its the correct one vboxmanage showvminfo vagrant_default_1566982938109_10062 # create a new box vagrant package --base vagrant_default_1566982938109_10062 --output devenv.v190819.box # add box to the known boxes vagrant box add devenv.v190819 devenv.v190819.box # list boxes and see the new box added to vagrant vagrant box list
Now you can reference the new box
# reference the new box ... Vagrant.configure("2") do |config| config.vm.box = "myjenkins" ...
Deleting VMs
vboxmanage list vms
"<inaccessible>" {5642f0cb-6042-486f-8ea6-7443cb5815b7} "Jenkins-Vagrant_default_1507401281630_21314" {8e3fd4ba-cda9-4539-9781-bbf7ad4fe423}
vvboxmanage unregistervm Jenkins-Vagrant_default_1507401281630_21314 --delete vboxmanage unregistervm Jenkins-Vagrant_default_1507401281630_21314 --delete
Commands
# clear the global-status cache, if a machine shows up, which is not running global-status --prune
Plugins
The complete list: https://github.com/hashicorp/vagrant/wiki/Available-Vagrant-Plugins
VBox additions
VBox additions installation. To enable folder mounting on any OS.
vagrant plugin install vagrant-vbguest vagrant vbguest
Winnfsd - helps handling NFS mounting problems on WIndows.
vagrant plugin install vagrant-winnfsd
VBox SCP
To copy files from the host to the guest.
vagrant plugin install vagrant-scp
Shell Provisioner
Echo multiple lines
mkdir -p /home/vagrant/.docker/ echo -e " { \"proxies\": { \"default\": { \"httpProxy\": \"http://localhost:8500\", } } } " > /home/vagrant/.docker/config.json
Connect via putty
Those scripts will use the vagrant configuration output, to connect to the vagrant machine via putty.
The normal way would be to install the special plugin, made for that :)
vagrant install plugin vagrant-multi-putty vagrant putty
If you want to go the long way, you can write an own script to do so
The putty client supports the full color palette and so has a better UX, than the CMD.
Put those files to that folder on the path, e.g. near the “vagrant.exe”
vagrantssh.ps1
A script to start the putty client and connect to the vagrant machine
$puttypath="D:\Programme\Putty\putty.exe" $vagrantProjectConfigs = & vagrant.exe ssh-config # parse the vagrant configs $port = ( $vagrantProjectConfigs | Select-String -Pattern "Port" -SimpleMatch ) -replace '^[ ]*Port[ ]+(.+)[ ]*', '$1' $identityfile = ( $vagrantProjectConfigs | Select-String -Pattern "IdentityFile" -SimpleMatch ) -replace '^[ ]*IdentityFile[ ]+(.+)[ ]*', '$1' $hostName = ( $vagrantProjectConfigs | Select-String -Pattern "HostName" -SimpleMatch ) -replace '^[ ]*HostName[ ]+(.+)[ ]*', '$1' & $puttypath vagrant@$hostName -pw vagrant -P $port -i "$identityfile"
vagrantssh.bat
A helper, to call the powershell script above.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%~dp0/vagrantssh.ps1'"
Useful plugins
Listed here https://github.com/hashicorp/vagrant/wiki/Available-Vagrant-Plugins
vagrant install plugin vagrant-multi-putty vagrant install plugin vagrant-scp vagrant install plugin vagrant-proxy