Provisioning a virtual machine using Vagrant involves setting up and configuring the software, packages, and settings within the virtual machine to match your project's requirements.
Provisioning can be done manually by logging into the VM and making the necessary changes, or it can be automated using configuration management tools like Ansible, Chef, Puppet or through shell scripts
1 . Create or Open the Vagrantfile
Navigate to your project directory and create a Vagrantfile if you don't already have one, or open your existing Vagrantfile in a text editor
2. Configure the Box and Provider
Set up the base box and provider configurations, as well as any other settings you need for your virtual machine.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
end
3 . Provisioning
3.1 Provisioning with Inline Script
Use the config.vm.provision
block to specify the inline provisioning script. You can use Shell scripting directly within the Vagrantfile for simple provisioning tasks
$shell = <<-SCRIPT
sudo apt-get update
sudo apt-get install -y apache2
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
config.vm.provision "shell", inline: $shell
end
3.2 Provisioning with external scripts
The shell provisioner can also take an option specifying a path to a shell script on the host machine. Vagrant will then upload this script into the guest and execute it.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
config.vm.provision "shell", path:"script.sh"
end
#!/bin/bash
# this is the script.sh file
sudo apt-get update
sudo apt-get install -y apache2
Approach (3.1 and 3.2 ) are suitable for small provisioning tasks, but for more complex configurations and multiple provisioners, it's recommended to use separate provisioning scripts or tools like Ansible, Chef, or Puppet.
3.3 Provisioning with Ansible
The provisioner can be also configured for Ansible provisioning
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/jammy64"
config.vm.provider "virtualbox" do |vb|
vb.memory = 2048
vb.cpus = 2
end
config.vm.provision :ansible do|ansible|
ansible.playbook = "playbook.yml"
end
end
---
- name: Installing apache2/httpd package through Ansible via Vagrant
hosts: all
become: true
tasks:
- name: Install apache httpd
ansible.builtin.apt:
name: apache2
state: present
4 . Start the Vagrant Environment
Run the following command to start the Vagrant environment and provision the virtual machine :
vagrant up
Vagrant will create the virtual machine, apply the provisioning steps defined in the config.vm.provision
block, and then launch the machine.
5 . Access the Virtual Machine
After provisioning, you can access the virtual machine via SSH using:
vagrant ssh
You can customize the provisioning script's to include any software installations, configuration changes, and other setup tasks required for your project.
Top comments (0)