Vagrant: Difference between revisions
From charlesreid1
| (12 intermediate revisions by the same user not shown) | |||
| Line 35: | Line 35: | ||
</pre> | </pre> | ||
From there you can run the vagrant init command to initialize the virtual machine using Vagrant | From there you can run the vagrant init command to initialize the virtual machine using Vagrant. | ||
'''NOTE:''' The first time you run the vagrant box with <code>vagrant up</code>, vagrant will download the virtual machine image, which could take a long time (10+ minutes) and will use a lot of bandwidth. | |||
<pre> | <pre> | ||
# for ubuntu 16.04: | |||
vagrant init ubuntu/xenial64 | vagrant init ubuntu/xenial64 | ||
vagrant up | |||
# for ubuntu 18.04: | |||
vagrant init ubuntu/bionic64 | |||
vagrant up | vagrant up | ||
</pre> | </pre> | ||
| Line 47: | Line 54: | ||
<pre> | <pre> | ||
vagrant ssh | |||
</pre> | </pre> | ||
To see the SSH details, type: | To see the SSH details, type: | ||
<pre> | |||
vagrant ssh-config | |||
</pre> | |||
'''NOTE:''' This command should be run from the host machine. | |||
The example output can be used verbatim in an ssh config file. For example: | |||
<pre> | <pre> | ||
| Line 67: | Line 82: | ||
</pre> | </pre> | ||
If there are multiple virtual machines, each node should have same values except for SSH key. | |||
If multiple virtual machines, each node should have same values except for SSH key. | |||
===Manually Connect to Vagrant Machine=== | ===Manually Connect to Vagrant Machine=== | ||
| Line 76: | Line 89: | ||
<pre> | <pre> | ||
ssh vagrant@127.0.0.1 -p 2222 -i $HOME/dev/ansible/dev/ansiblebook/ch01/playbooks/.vagrant/machines/default/virtualbox/private_key | |||
</pre> | |||
or, for a shorter version: | |||
<pre> | |||
vagrant ssh-config > /tmp/vconf && ssh -F /tmp/vconf default | |||
</pre> | |||
or even shorter, | |||
<pre> | |||
vagrant ssh | |||
</pre> | </pre> | ||
This will log you in without prompting for a password. | This command: | ||
* exports the vagrant configuration to a file | |||
* specifies the configuration file for ssh to use with the <code>-F</code> flag | |||
* specifies which configuration to use (default) | |||
Both of these SSH commands will log you in without prompting for a password. | |||
Note that everything vagrant-related is stored in the <code>.vagrant/</code> directory. | Note that everything vagrant-related is stored in the <code>.vagrant/</code> directory. | ||
| Line 88: | Line 119: | ||
<pre> | <pre> | ||
vagrant halt | |||
</pre> | </pre> | ||
=Using Vagrant with Multiple Machines= | '''NOTE:''' This should be run from the directory containing the Vagrantfile | ||
=Using Vagrant with Multiple Virtual Machines= | |||
It is possible to run multiple virtual machines using vagrant. | |||
See the guide on the [https://charlesreid1.com/wiki/Ansible/Full_Stack_Playbook#Vagrant_multi-machine_setup Ansible/Full Stack Playbook] page for a guide. | |||
=Create Vagrant Box from Virtual Box= | |||
If you want to customize your vagrant image or create a new vagrant box from an .iso file on disk, you can create a VirtualBox machine from the iso image (and customize it however you'd like), then convert the VirtualBox box into a vagrant box. | |||
See procedure here: [[Vagrant/Boxes]] | |||
The basic summary: | |||
* Create the virtual machine image in Virtual Box, and get it all set up the way you want | |||
* Package the VirtualBox image into vagrant box format, so you can reference it from a Vagrantfile | |||
* Tell vagrant to use the new box, which can be done two ways: | |||
** Method One: create the vagrant box file, and then set the URL to the vagrant box file in the Vagrantfile, using the directive <code>config.vm.box_url = "file:mycoolbox"</code> | |||
** Method Two: create the vagrant box file, and import the box into vagrant so it knows about the new machine image available to it | |||
=Resources= | =Resources= | ||
| Line 101: | Line 150: | ||
=Flags= | =Flags= | ||
{{VagrantFlag}} | |||
Latest revision as of 01:06, 10 December 2019
Vagrant is an open source tool for managing virtual machines.
Related: Ansible
What is it
Think of Vagrant like a command line interface for VirtualBox.
Vagrant has built-in support for Ansible.
Installing
Start by installing VirtualBox, then install Vagrant from http://www.vagrantup.com/
Mac OS X
On OS X, use Homebrew to install both VirtualBox and Vagrant:
# NOTE: These commands will ask you for your system password brew cask install virtualbox brew cask install vagrant brew cask install vagrant-manager
Basic Startup Shutdown Procedure
Start Vagrant Machine
Create a directory for your vagrant virtual boxes:
mkdir myvagrant cd myvagrant
From there you can run the vagrant init command to initialize the virtual machine using Vagrant.
NOTE: The first time you run the vagrant box with vagrant up, vagrant will download the virtual machine image, which could take a long time (10+ minutes) and will use a lot of bandwidth.
# for ubuntu 16.04: vagrant init ubuntu/xenial64 vagrant up # for ubuntu 18.04: vagrant init ubuntu/bionic64 vagrant up
Connect to Vagrant Machine
Now you can ssh into the new virtual machine using
vagrant ssh
To see the SSH details, type:
vagrant ssh-config
NOTE: This command should be run from the host machine.
The example output can be used verbatim in an ssh config file. For example:
$ vagrant ssh-config Host default HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /Users/whoami/dev/ansiblebook/ch01/playbooks/.vagrant/machines/default/virtualbox/private_key IdentitiesOnly yes LogLevel FATAL
If there are multiple virtual machines, each node should have same values except for SSH key.
Manually Connect to Vagrant Machine
using the key printed from the above command, you can SSH manually using that key:
ssh vagrant@127.0.0.1 -p 2222 -i $HOME/dev/ansible/dev/ansiblebook/ch01/playbooks/.vagrant/machines/default/virtualbox/private_key
or, for a shorter version:
vagrant ssh-config > /tmp/vconf && ssh -F /tmp/vconf default
or even shorter,
vagrant ssh
This command:
- exports the vagrant configuration to a file
- specifies the configuration file for ssh to use with the
-Fflag - specifies which configuration to use (default)
Both of these SSH commands will log you in without prompting for a password.
Note that everything vagrant-related is stored in the .vagrant/ directory.
Stop Vagrant Machine
To stop the machine, run vagrant halt:
vagrant halt
NOTE: This should be run from the directory containing the Vagrantfile
Using Vagrant with Multiple Virtual Machines
It is possible to run multiple virtual machines using vagrant.
See the guide on the Ansible/Full Stack Playbook page for a guide.
Create Vagrant Box from Virtual Box
If you want to customize your vagrant image or create a new vagrant box from an .iso file on disk, you can create a VirtualBox machine from the iso image (and customize it however you'd like), then convert the VirtualBox box into a vagrant box.
See procedure here: Vagrant/Boxes
The basic summary:
- Create the virtual machine image in Virtual Box, and get it all set up the way you want
- Package the VirtualBox image into vagrant box format, so you can reference it from a Vagrantfile
- Tell vagrant to use the new box, which can be done two ways:
- Method One: create the vagrant box file, and then set the URL to the vagrant box file in the Vagrantfile, using the directive
config.vm.box_url = "file:mycoolbox" - Method Two: create the vagrant box file, and import the box into vagrant so it knows about the new machine image available to it
- Method One: create the vagrant box file, and then set the URL to the vagrant box file in the Vagrantfile, using the directive
Resources
Mac OS X Setup Guide: https://sourabhbajaj.com/mac-setup/Vagrant/README.html