Hey! I have been learning Ansible to automate server setup. But I was a bit afraid to run my playbook directly on the server. So I thought better is to run it on a virtual machine via vagrant. And if everything works fine here, then I can run it against my server. But the problem is, I never worked with vagrant before. Here, in this article I would start and document my learning path for vagrant. Let’s do it.
My use case is that, I just need a VirtualBox running in which I can SSH. That box should preferably be of Ubuntu, that’s it. I don’t want any more customisation so I won’t get into those complexities of vagrant configuration files in depth.
TLDR
brew install vagrant
brew install virutalbox
vagrant box add ubuntu/focal64
vagrant init ubuntu/focal64
vagrant up
vagrant ssh
The long way
Let’s first read a bit about what vagrant is from their official docs: #1 https://www.vagrantup.com/intro
That #1 was really brief, we obviously need more. Let’s go for practice: #2 https://learn.hashicorp.com/tutorials/vagrant/getting-started-index
What I did:
brew install vagrant
vagrant --version
Vagrant 2.2.10
vagrant box add ubuntu/focal64
vagrant box list
ubuntu/focal64 (virtualbox, 20201210.0.0)
vagrant init ubuntu/focal64
Now I could notice a “Vagrantfile” in my project directory (from where I ran the above commands). Let’s try vagrant up.
vagrant up
This showed me an error that I would have to installed a third party virtual machine (VM) provider such as VMware, VirtualBox, etc. So I did:
brew install virtualbox
...
<asked me for my password>
...
NOTE:
- Now, I was taken to System Preferences -> Security & Privacy -> General
- I needed to enable the extension installation from VirtualBox Oracle. As explained in this post: https://medium.com/@Aenon/mac-virtualbox-kernel-driver-error-df39e7e10cd8
Now, when I did vagrant up
the virtual machine started working (running).
The next step was to “SSH” into it, and as written in their docs the command is vagrant ssh
. It was successfully executed.
But I didn’t want to manually get SSH into vagrant using vagrant ssh
. Instead, I wanted my ansible to SSH into this running VM. For that it was necessary to setup my ssh config file ~/.ssh/config
. I add the following in it.
Host my-project
User vagrant
HostName 127.0.0.1
Port 2222
IdentityFile /path/to/my-project-directory/.vagrant/machines/default/virtualbox/private_key
And saving this file. Now running: ssh my-project
was leading me to my VM. I also tried
vagrant halt
– to shutdown the VM
vagrant suspend
– to suspend the VM
vagrant resume
– to wake VM up from suspend state
vagrant status
– to check the status of current VM (current means the box triggered by the current Vagrantfile in the directory)
vagrant global-status
– to check the status of all the VMs on the system.
As my ssh
could connect to my running VM, that means, my ansible can also connect. I tried running simple ansible module “ping” on this VM. (file “hosts” contains just one pattern, it should match with your ssh config file host)
cat hosts
my-project
ansible all -i hosts -m "ping"
my-project | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
Cool! Now I can run my playbook on this VM without worry. Following are some more helpful resources:
- https://codereviewvideos.com/course/vagrant-with-ansible-and-symfony2/video/beginners-guide-to-vagrant
- https://stackoverflow.com/questions/10864372/how-to-ssh-to-vagrant-without-actually-running-vagrant-ssh
Thank you!
One thought on “Vagrant Quickstart – Live Learning Notes”