Skip to content
Home » Automate VM Setup with Vagrantfile

Automate VM Setup with Vagrantfile

Vagrant is a powerful tool that allows developers to easily create and manage virtual environments. This tutorial covers the basics of setting up a Vagrantfile using both Ubuntu and Rocky Linux examples, with a focus on practical use-cases for power-users and advanced users.

Prerequisites

Before you begin, you must have a hypervisor like Hyper-V or VirtualBox installed together with Vagrant. If you’re using Window then I recommend using Hyper-V for a better compatibility.

Finding and Using Vagrant Boxes

For finding pre-packaged Vagrant boxes use https://app.vagrantup.com/boxes/search. This website allows you to search for boxes based on the operating system, provider, and other criteria. Once you’ve found a box that you like, you can easily copy the Vagrantfile code and customize it to meet your needs.

Another way to find and use Vagrant boxes is by using the vagrant init command. This command creates a basic Vagrantfile in your current directory and allows you to specify the box that you want to use. For example, to create a Vagrantfile for the Rocky Linux box, you would run the following command:

vagrant init generic/rocky9

Ubuntu Example

Here’s an example of a basic Vagrantfile that sets up a virtual environment using Ubuntu:

Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2010"
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.provider "hyperv" do |vb|
    vb.memory = "2048"
  end
end

In this example, the virtual environment will use the ubuntu/bionic64 box, which is a pre-packaged version of Ubuntu. The virtual environment will also have a private network with the IP address 192.168.33.10 and a memory of 2048 MB.

Rocky Linux Example

Here’s an example of a basic Vagrantfile that sets up a virtual environment using Rocky Linux:

Vagrant.configure("2") do |config|
  config.vm.box = "generic/rocky9"
	config.vm.network "private_network", ip: "192.168.33.11"
  config.vm.provider "hyperv" do |vb|
	  vb.memory = "2048"
  end
end

In this example, the virtual environment will use the generic/rocky9 box, which is a pre-packaged version of Rocky Linux. The virtual environment will also have a private network with the IP address 192.168.33.11 and a memory of 2048 MB.

5 Tips for Using Vagrant

  1. Use version control for your Vagrantfile. This allows you to easily roll back changes and collaborate with others.
  2. Keep your virtual environment as simple as possible. The fewer components you have installed, the less chance there is of a security vulnerability.
  3. Make sure to regularly backup your virtual environment. In the event of a crash or data loss, this will save you time and headaches.
  4. Use provisioning scripts to automate the setup and configuration of your virtual environment. This saves time and ensures consistency between environments.
  5. Test your virtual environment regularly to ensure that it’s functioning as expected and that there are no security vulnerabilities.

Vangrantfile With Provisioning Settings

# Use the generic/rocky9 box
Vagrant.configure("2") do |config|
  config.vm.box = "generic/rocky9"

  # Assign a static IP address
  config.vm.network "private_network", ip: "192.168.33.10"

  # Forward port 80 to the host's port 8080
  config.vm.network "forwarded_port", guest: 80, host: 8080

  # Automatically run the setup script after provisioning
  config.vm.provision "shell", path: "setup.sh"

  # Enable the use of the host's local DNS server
  config.vm.network "private_network", dns: "8.8.8.8"

  # Use NFS for shared folders for better performance
  config.vm.synced_folder ".", "/vagrant", type: "nfs"

  # Set the hostname to "itvraag.nl"
  config.vm.hostname = "itvraag.nl"
end

Summary

In conclusion, Vagrant is a powerful tool that allows developers to easily create and manage virtual environments. By setting up a Vagrantfile, you can specify the details of your virtual environment, including the type of operating system to use, network settings, and memory allocation. When using Vagrant, it’s important to consider security risks and take steps to minimize these risks. By following the tips provided in this tutorial, you can ensure that your virtual environment is secure, efficient, and effective.

For those who want to continue learning about Vagrant, I recommend exploring the following topics:

  • Provisioning scripts
  • Firewall and network security configurations
  • Sharing virtual environments with others
  • Advanced network configurations using Vagrant

As a final challenge, try setting up a complex virtual environment using Vagrant and experimenting with different configuration options.

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen + twelve =