Chukwuma Zikora
Software Engineer. RPA and DevOps Enthusiast
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.
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:
- Define and provision a local development environment using Vagrant,
- 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
Step 2: Initialize a Vagrantfile
Run the following command to create a default Vagrantfile
:
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:
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:
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:
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
. Thisconfig.vm.provision
section executes the Ansible playbookprovision.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:
Inside the VM:
-
Check Apache Status:
-
Debug Common Issues:
- If provisioning fails, re-run Ansible with:
- Check the Ansible logs for detailed errors.
-
View the Website: If the Apache server is running, run the following command to view the website:
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.
- Get the VM IP Address:
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
:
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
.
- Stop the VM: To stop the VM, run the following command:
To remove the VM and its associated files, run the following command:
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.