Chukwuma Zikora

Chukwuma Zikora

Software Engineer. RPA and DevOps Enthusiast

IaC, Vagrant, Ansible, DevOps, Automation

Part 1: Automating Local Environment Setup with Vagrant and Ansible

Infrastructure as code, or IaC, is a set of practices that allows developers to automate the creation, configuration, and management of infrastructure. In this article, we will explore how to use Vagrant and Ansible to create a local development environment.

Part 1: Automating Local Environment Setup with Vagrant and Ansible

In modern software development, Infrastructure as Code (IaC) simplifies creating, configuring, and maintaining environments. Tools like Vagrant and Ansible allow developers to automate the setup of local development environments, ensuring consistency and reducing manual errors.

Introducing Vagrant and Ansible

Vagrant is a powerful tool for managing virtual machines (VMs). It simplifies the process of creating and maintaining reproducible development environments. With Vagrant, developers can use configuration files to define and provision environments that closely mimic production servers. This ensures smoother transitions from local testing to deployment and reduces the risk of inconsistencies.

Ansible is an automation tool designed for simplicity and efficiency. It uses human-readable YAML files called "playbooks" to automate tasks such as software installation, configuration management, and application deployment. Ansible’s agentless architecture allows it to connect to servers using SSH, keeping resource usage minimal while maintaining high performance.

Together, these tools provide a solution for creating, managing, and provisioning consistent environments across development and production servers.


By the end of this article, you will learn how to:

  1. Define and provision a local development environment using Vagrant,
  2. Handle server configuration, dependencies installation, and application deployment using Ansible,

Prerequisite

To get started, you need Vagrant , VirtualBox, and Ansible installed. For installation instructions, refer to their official websites:

Step 1: Create a Project Directory

mkdir vagrant_ansible_project
cd vagrant_ansible_project

Step 2: Initialize a Vagrantfile

Run the following command to create a default Vagrantfile:

vagrant init

The default Vagrantfile provides a basic configuration for a standard Vagrant box. It includes several commented-out features, such as synced folders, networking options, and provisioning examples using a SHELL script. These options can be enabled as needed for more advanced use cases.

We will now clear out the default configuration and add the necessary settings for our use case.

Replace the Vagrantfile with the following content:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
 
  config.vm.provider "virtualbox" do |vb|
    vb.name = "vm_ubuntu_22_04"
  end
 
  config.vm.network "private_network", type: "dhcp"
 
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provision.yml"
  end
end
 

This Vagrantfile:

  • Sets the base box to ubuntu/jammy64. Which means we are creating a VM with Ubuntu 22.04 operating system.
  • Sets the provider to VirtualBox. This is the default provider, but you can use other providers like VMware, Hyper-V, or Parallels.
  • Sets the network to private_network with DHCP. This means the VM will have a private IP address and will be accessible only within the same network.
  • Sets the provisioner to Ansible. This means the Vagrantfile will execute the Ansible playbook provision.yml to configure the VM once it is up and running.

Step 3: Write the Ansible Playbook

Next we will create a file named provision.yml in the project directory and write the Ansible playbook:

---
- name: 'Provision VM'
  hosts: all
  become: true
 
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
 
    - name: Install Apache
      apt:
        name: apache2
        state: present
 
    - name: Start Apache
      service:
        name: apache2
        state: started

This playbook:

  • Updates the package cache.
  • Installs the Apache web server.
  • Starts and enables the Apache service.

Step 4: Start the Vagrant VM

Run the following commands to launch and provision the VM:

vagrant up

This command:

  • Downloads the base box, ubuntu 22.04 (if not already downloaded). You can use any other box as well. To view the list of available boxes, run vagrant box list.
  • Sets up the VM according to the Vagrantfile. This config.vm.provision section executes the Ansible playbook provision.yml to provision the VM.

Step 5: Verify the Ansible Provisioning

Once the VM is running, we can SSH into it to verify the setup:

vagrant ssh

Inside the VM:

  1. Check Apache Status:

    systemctl status apache2
  2. Debug Common Issues:

    • If provisioning fails, re-run Ansible with:
      vagrant provision
    • Check the Ansible logs for detailed errors.
  3. View the Website: If the Apache server is running, run the following command to view the website:

curl http://localhost

You should see the default Apache test HTML page's content. To view this on your host machine you will need the IP address of the VM.

  1. Get the VM IP Address:
ip address | grep "inet "

Run the above command while inside the vm. Look for the line similar to inet 192.168.56.6/24 brd 192.168.56.255 scope global dynamic eth0. Copy the IP address from the inet line and open it in your system browser. You should see the Apache test page.

We can also give the VM a static IP address by adding the following line to the Vagrantfile:

# replace
config.vm.network "private_network", type: "dhcp"
 
# with
config.vm.network "private_network", ip: "192.168.56.6"

For this to take effect, we need to run vagrant reload to restart the VM. Now you can view the Apache test page on your host machine at http://192.168.56.6.

  1. Stop the VM: To stop the VM, run the following command:
vagrant halt

To remove the VM and its associated files, run the following command:

vagrant destroy

Conclusion

You’ve successfully automated the setup of a local development environment using Vagrant and Ansible. This foundational setup ensures consistency and scalability as we move toward production deployments.

In the next part, we will explore how to server a static website with Apache on our VM. This will replace the default Apache test page with a custom website.

You can access the repository for the series using this link.