Vagrant enables users to create and configure lightweight, reproducible, and portable development environments. This weekend I really needed something like this. Like really did. However I didn’t want to write my provisioning in shell scripts. I mean we are in 2016! Also handling provisioning using shell scripts suck.

Here comes our hero Ansible. Ansible manages machines in an agent-less manner. It uses SSH to connect to the machines and runs the Ansible scripts. Which are in YML format. Which is pretty awesome. Read more of it and have fun with it!

Vagrant gives you the opportunity, nay privilege! to use Ansible for provisioning your Vagrant box. You can do it like below;

# -*- mode: ruby -*-
# vi: set ft=ruby : 

Vagrant.configure(2) do |config|
    config.vm.box = "ubuntu/wily64" 
    config.vm.box_check_update = true
    config.vm.network "forwarded_port", guest: 8085, host: 8085
    config.vm.network "forwarded_port", guest: 8000, host: 8000
    config.ssh.forward_agent = true
    config.vm.synced_folder "project_folder/", "/home/vagrant/project_folder", create: true 

    config.vm.provision "ansible" do |ansible|
       # LOOK! ANSIBLE HERE!! ansible.playbook =    
       "ansible/setup.yml" ansible.verbose = "vvvv"
    end 
    config.vm.provider "virtualbox" do |v|
        v.name = "project-box"
        v.memory = 6000
        v.cpus = 4
     end
end

So what does setup.yml do? Of course the most things ever, like setting up a full blown Django development box that has RabbitMQ, Redis and MySQL. Got help from Ansible-Galaxy for some of that stuff. Don’t know what Ansible-Galaxy is? It is pure awesomeness, OpenSource WebOps stuff. You will like it check it out.

So you might ask, what was the SSH issue that I had mentioned in the title. So the thing is that my synced_folder was pointing to the home vagrant directory. Which is a big no no… I started getting the error below when the provisioning started;


fatal: [default] => SSH Error: Permission denied (publickey,password). while connecting to 127.0.0.1:2222
     It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue. 
< TASK: base_install | install required packages > 
------------------------------------------------ 
FATAL: no hosts matched or all hosts have already failed -- aborting

Once I changed the synced_folder to the project folder instead of vagrant folder everything worked!

As always have fun!