Vagrant & Ansible. Or how to make your life better as a developer.

  • Fri 10 April 2015

  • I'm a huge proponent of local development in an environment that matches production. One way to do this is to leverage Vagrant and the same configuration management you use to provision production to provision a local virtual machine. You do use configuration management in production right? RIGHT?!

    As a sys admin, I'm amazed how many developers who don't use or have never heard of Vagrant. It is an incredible tool that makes development so much easier. The manner in which I develop completely changed and my life is forever better because of it.

    So I want to put this here for others.

            # -*- mode: ruby -*-
            # vi: set ft=ruby :
    
            VAGRANTFILE_API_VERSION = "2"
    
            Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
              config.vm.define "rabbitmq1" do |rabbitmq1|
                    config.vm.hostname = "master"
                    rabbitmq1.vm.box = "trusty64"
                    rabbitmq1.vm.network :private_network, ip: "192.168.33.10"
                    rabbitmq1.vm.network "forwarded_port", guest: 5672, host: 5672
                    config.vm.provision "ansible" do |ansible|
                            ansible.playbook = "ansible/rabbitmq.yml"
                            ansible.groups = {
                            "rabbitmq" => ["rabbitmq1"],
                            }
                    end
              end
    
              config.vm.define "redis1" do |redis1|
                    config.vm.hostname = "master"
                    redis1.vm.box = "trusty64"
                    redis1.vm.network :private_network, ip: "192.168.33.11"
                    redis1.vm.network "forwarded_port", guest: 6379, host: 6379
                    config.vm.provision "ansible" do |ansible|
                            ansible.playbook = "ansible/redis.yml"
                            ansible.groups = {
                            "redis" => ["redis1"],
                            }
                    end
              end
              config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
            end
    

    How do you use it? (tl;dr)

    • To check state of virtual machines: vagrant status
    • To ssh into a virtual machine: vagrant ssh $hostname
    • To start virtual machines: vagrant up
    • To stop virtual machines (non-destructive): vagrant halt
    • To stop virtual machines (destructive): vagrant destroy
    • To provision virtual machines: vagrant provision

    I trust you'll read the docs. They're actually quite thorough. But with a simple vagrant up, the virtual machine will boot and once it has booted, vagrant will call Ansible and run the playbook defined in the vagrantfile. You can make changes to your playbook/roles and after saving you can run vagrant provision to run the playbook on the hosts.

    Because I'd imagine you're curious what the playbook might look like, here it is:

    ---
    # file: redis.yml
    - hosts: redis
      sudo: yes
      roles:
        - base
        - redis
    
    ---
    # file: rabbitmq.yml
    - hosts: rabbitmq
      sudo: yes
      roles:
        - base
        - rabbitmq
    

    It's awesome!

    Enjoy!!

    Comments !

    social