In this article, we will discuss how to use Vagrant and Ansible for provisioning virtual servers managed by Hyper-V on Windows 11. We will cover key concepts, including enabling WSL2, installing necessary software, setting up a Vagrantfile, and writing an Ansible playbook for provisioning.
Prerequisites
Before we begin, ensure that you have the latest version of Vagrant (2.4.0) installed on your Windows 11 machine. Additionally, you will need to enable WSL2, which can be done by following these steps:
- Open PowerShell as Administrator and run the following command:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
- Restart your machine.
- After restarting, open PowerShell as Administrator and run the following command:
wsl --install
- Restart your machine again.
Once WSL2 is enabled, you can proceed with installing Ubuntu from the Microsoft Store.
Installing VirtualBox and Vagrant
For this tutorial, we will use VirtualBox as our hypervisor, so install it from the official website.
- Download and install VirtualBox from VirtualBox Downloads.
After installing VirtualBox, you can proceed with installing Vagrant.
- Download and install Vagrant from Vagrant Downloads.
Setting up a Vagrantfile
Now that you have installed VirtualBox and Vagrant, you can proceed with setting up a Vagrantfile. Create a new directory and initialize a new Vagrant environment inside it:
mkdir my-project && cd my-project
vagrant initEdit the Vagrantfile by adding the following lines:
Vagrant.configure("2") do |config| config.vm.box = "ubuntu/focal64" config.vm.provider "virtualbox" do |vb| vb.memory = "2048" vb.cpus = "2" vb.customize ["modifyvm", :id, "--boot1", "disk", "--boot2", "dvd", "--boot3", "none", "--boot4", "none"] end config.vm.network "private_network", ip: "192.168.50.4" config.vm.provision "ansible" do |ansible| ansible.playbook = "playbook.yml" end endThis Vagrantfile sets up an Ubuntu Focal 64-bit box with 2GB of memory and 2 CPUs. It also provisions the box using Ansible and runs the playbook specified in playbook.yml.
Writing an Ansible Playbook for Provisioning
Now that your Vagrantfile is set up, you can proceed with writing an Ansible playbook for provisioning. Create a new file called "playbook.yml" in the same directory and add the following lines:
--- - hosts: all become: yes tasks: - name: Update packages apt: update_cache: yes upgrade: yes - name: Install necessary packages apt: name: [``` "python3", "software-properties-common", "curl"] - name: Add Ansible repository apt_repository: repo: ppa:ansible/ansible state: present - name: Install Ansible apt: name: ansible state: present ```This playbook installs necessary packages, adds the Ansible repository, and installs Ansible itself.
Running Vagrant with Ansible Provisioning
Now that your Vagrantfile and Ansible playbook are set up, you can proceed with running Vagrant with Ansible provisioning.
vagrant upThis will start a new VirtualBox VM based on the Ubuntu Focal 64-bit box and run the Ansible playbook specified in the Vagrantfile. Once provisioning is complete, you can SSH into the VM using the following command:
vagrant sshIn this article, we covered how to use Vagrant and Ansible for provisioning virtual servers managed by Hyper-V on Windows 11. We discussed enabling WSL2, installing necessary software, setting up a Vagrantfile, and writing an Ansible playbook for provisioning. By following these steps, you can easily set up a development environment for your projects.
References