Development Environment setup using Vagrant and Ansible


In this article, I will demonstrate how to set up a local development environment with Vagrant and ansible. Before starting, I will briefly explain what is vagrant and Ansible.


Vagrant is a command-line utility for managing the lifecycle of virtual machines. It is a tool for working with virtual environments. You can create, delete virtual machines easily. Vagrant will take care of downloading, installing the OS and modifying the environment as per our requirement. Vagrant can be installed on Windows, Linux, and macOS.

Vagrant makes it easy to create a local environment that mimics the production in which your code will eventually be deployed. You can make sure you have the same libraries and dependencies installed, same processes installed, same operating system and version, and many other details without having to sacrifice the way your local machine is set up, and without the lag or cost of creating an external development environment and connecting to it.


Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, infrastructure orchestration, and provisioning. Ansible is a clientless application, which means you don’t need to install agents on each node. Ansible works with SSH for Linux machines and also ansible support windows machine configurations.


  • Windows/Linux/Mac machine
  • Vagrant installed
  • VirtualBox installed
  • Folders for Local vagrant files

Step – 1 vagrant Configurations

Install vagrant and virtual box on your system

  • Create a folder called local-vagrant and cd to local-vagrant
  • Create a file called Vagrantfile.
  • Create an ansible folder and playbook.yml file in it

This will be the Tree structure

$ local-vagrant: $ tree
├── Vagrantfile
└── ansible
    └── playbook.yml
  • Copy and paste the below content to vagrant file
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

    #Ubuntu VM
    config.vm.box = "ubuntu/trusty64"
    config.vm.network "private_network", ip: ""

    config.vm.provider "virtualbox" do |v|
        v.name = "Test-VM"
        v.memory = 4096

    # Mount the current directory for configuration
    config.vm.synced_folder ".", "/vagrant/environment"

    # Mount the projects directory. 
    # Preferred way: set an environment variable named 'VAGRANT_SERVICES_DIR' pointing to your local services directory.
    # Alternative way: replace <your services root dir> with your local services directory (will be overwritten when updating).
    services_dir = ENV['VAGRANT_SERVICES_DIR'] || '<your services root dir>'
    config.vm.synced_folder  services_dir, "/vagrant/services"

config.vm.provision "ansible_local" do |ansible|
      ansible.compatibility_mode = "2.8"
      ansible.playbook = "/vagrant/environment/ansible/playbook.yml"


Now we have created the necessary files for setting up the environment.

Start the machine by running vagrant up, Vagrant will download ubuntu/trusty64 image from the vagrant cloud and boot to the virtual machine with the configurations we specified in the vagrant file.

config.vm.box: Distribution name. you can choose any image you like from vagrant cloud

config.vm.network: IP address to access the virtula machine. Default vagrant will create a host only adapter. If you need to change to bridged adapter, change private_network to public_network

v.name Name of the Virtual machine

v.memory: Memory allocated to VM

config.vm.synced_folder: Local PC directory to be synced with Vagrant VM. You can specify mulitple Directories

config.vm.provision: Virtual machine provision tools. Here we are using ansible for provisioning the server. You can choose any configuration management tool or shell of your choice.

ansible.playbook: Playbook used for provisioning the VM

Step 2 Ansible playbook – Provisioner

Let’s configure the ansible-playbook the way you need to set up the VM. I am going to install an apache2 only.

- hosts: all
  become: yes
    - name: Install required packages
        name: "{{ item }}"
        state: installed
        - apache2
    - name: Start the Apache2 service
        name: apache2
        state: started
        enabled: yes

Step 3 – Starting the VM

Start the VM by running vagrant up

vagrant up
==> vagrant: A new version of Vagrant is available: 2.2.6 (installed version: 2.2.3)!
==> vagrant: To upgrade visit: https://www.vagrantup.com/downloads.html

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'bento/ubuntu-18.04'

If you are running this for the first time, the vagrant will download the specified image from the vagrant cloud and boot the VM and run the provisioner. So you will get a VM ready to work on

To SSH to the vagrant machine, run vagrant ssh.


by default, vagrant won’t ask for password. If the console prompting for password, please enter vagrant as password

Now let’s test the apache installation is successful, please open your browser in your host machine and type the Vagrant IP address specified in Vagrantfile

You can see the apache default home page. Our installation is successful.

If you want to stop the Vagrant machine, run vagrant halt

Some Useful Vagrant Commands

  • vagrant init – Initialize default vagrant file
  • vagrant up – Start the VM
  • vagrant halt – Stop the VM
  • vagrant provision – Run the provisioner only
  • vagrant up --provision – Start the VM and Run the provisioner
  • vagrant destroy – destroy the vagrant environment



Check out these books from amazon

Leave a Reply