updated: 20th of January 2019
published: 14th of January 2019
I discovered a nice little trick to make working with Vagrant VMs a little easier by utilizing the openssh config file to define the SSH connection parameters for Vagrant VMs. This post will walk you through getting it setup.
For reference the following software will be used in this post.
In order to utilize this method you will need to have openssh version 7.3p1 or greater. If you are not sure what an SSH config file is or does, go on a quick duck hunt there are a plethora for resources out there.
Usually when working with Vagrant you access a VM by using the command vagrant ssh <vm-name> . This is fine, but things start to get messy when you want to use Ansible or Puppet Bolt etc.. to apply config to the VMs. You either need to use Vagrants config to apply your inventory groupings or twiddle around with your inventory files.
Vagrant has a command vagrant ssh-config that will spit out a valid openssh config file. This file can be used with openssh to define ssh connection parameters for your Vagrant VMs.
First up be sure you have at least openssh version 7.3p1
ssh -V # output OpenSSH_7.6p1 Ubuntu-4ubuntu0.1, OpenSSL 1.0.2n 7 Dec 2017
Next up create a directory for your vagrant SSH config.
mkdir -p ~/.ssh/vagrant.d/
The Include parameter was added to openssh in version 7.3p1 . Add the following to your ~/.ssh/config file.
Now navigate to the directory where your Vagrant lab is configured and run the following command.
vagrant ssh-config > ~/.ssh/vagrant.d/sshconfig
This will take the Vagrant SSH config and add it to your home directory SSH config.
Now instead of using the vagrant ssh <vm-name> command to access your VMs you can just use ssh <vm-name> . Not only will this be alot faster than using the vagrant ssh you wont have to mess around with /etc/hosts file or do funny tricks with your configuration management inventory files.
ssh lab-spine01 # output Welcome to Cumulus VX (TM) Cumulus VX (TM) is a community supported virtual appliance designed for experiencing, testing and prototyping Cumulus Networks' latest technology. For any questions or technical support, visit our community site at: http://community.cumulusnetworks.com The registered trademark Linux (R) is used pursuant to a sublicense from LMI, the exclusive licensee of Linus Torvalds, owner of the mark on a world-wide basis. vagrant@cumulus:~$
cat ~/.ssh/vagrant.d/sshconfig # output Host lab-spine01 HostName 192.168.121.114 User vagrant Port 22 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /home/bradmin/.vagrant.d/insecure_private_key IdentitiesOnly yes LogLevel FATAL
There is an alternate method which is not as seamless but is available if you are running openssh < 7.3p1 or prefer not to alter your user ssh config.
This method involves redirecting the vagrant ssh-config command output to a local directory file (.sshconfig in the below example).
vagrant ssh-config > .sshconfig
Then use the -F flag to specify the ssh config file for the ssh connection.
ssh -F .sshconfig lab-spine01
SSH config is a nice way to manage your SSH connections. In my $JOB I rely on my ssh config to navigate around the network through a multitude of jump boxes. Using an SSH config to manage the Vagrant lab I am working on is a nice little hack and I am kicking myself I did not think of it sooner.