➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Vagrant provisioning is the process of automatically setting up the software and configuration needed for a virtual machine (VM) when it is created or started. This can include installing packages, configuring services, running scripts, and setting up environments. Provisioning ensures that the VM is prepared and ready to use according to specified requirements. Provisioning can also be applied when the VM is already running.
Types of Provisioners in Vagrant
- Shell Provisioner: Runs shell scripts to configure the VM.
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
SHELL
- File Provisioner: Copies files from the host machine to the guest VM.
config.vm.provision "file", source: "localfile.txt", destination: "~/remotefile.txt"
- Puppet Provisioner: Uses Puppet to manage configurations.
config.vm.provision "puppet" do |puppet|
puppet.manifest_file = "default.pp"
end
- Chef Provisioner: Uses Chef recipes to configure the VM.
config.vm.provision "chef_solo" do |chef|
chef.cookbooks_path = "cookbooks"
chef.add_recipe "my_cookbook::default"
end
- Ansible Provisioner: Uses Ansible playbooks to manage configurations.
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
Benefits of Provisioning
- Automation: Reduces manual setup and ensures consistency across environments.
- Reproducibility: Easily recreate environments with the same setup.
- Scalability: Quickly scale configurations across multiple VMs.
By using provisioning, you can automate the setup and configuration of your Vagrant environments, making it easier to manage and maintain them.
Vagrant Provisioning Example
First, ensure that the Ubuntu VM is created and the CentOS VM is not. We’ll provide provisioning examples for a CentOS VM (which is not created) and an already-created Ubuntu VM.
$ vagrant global-status
id name provider state directory
-------------------------------------------------------------------------
600b51c default virtualbox poweroff C:/install/vagrant-vms/ubuntu
Open the Vagrantfile for CentOS, uncomment the following lines, and modify the IP ensuring the same IP is not allocated to the Ubuntu VM.
config.vm.network "private_network", ip: "192.168.56.99"
config.vm.network "public_network"
# Present at the end of the file
config.vm.provision "shell", inline: <<-SHELL
# commands for centos
yum install httpd wget unzip git -y
mkdir /opt/devopsdir
free -m
uptime
SHELL
Adding -y
in yum install httpd wget unzip git -y
is crucial because it automates the installation by bypassing interactive prompts that would otherwise cause the provisioning to fail.
Save the Vagrantfile, go to the CentOS directory, and start the VM.
$ cd ../centos
$ ls
Vagrantfile
$ tail Vagrantfile
$ vagrant up
If the commands generate any output, you’ll see it at the end. Note that provisioning is done only once when the VM is created. If you reload Vagrant using vagrant reload
, these commands won’t be executed again.
$ vagrant reload
# In the logs we can find:
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
Now, let’s do the same for an already-created Ubuntu VM. Edit its Vagrantfile:
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y apache2
free -m
uptime
SHELL
Navigate to the Ubuntu folder, check the Vagrantfile and provision:
$ cd ../ubuntu
$ tail Vagrantfile
$ vagrant status
$ vagrant reload --provision
SSH into Vagrant and verify the installation. If config.vm.network "public_network"
and config.vm.network "private_network", ip: "192.168.56.10"
were enabled (or you uncomment/modify them and apply vagrant reload --provision
), you can get the IP and check the default Apache2 page on the host machine.
$ vagrant ssh
$ hostname -I
In the host machine, enter the IP 192.168.56.10
in the browser to see the Apache2 Default Page from Ubuntu. In CentOS, by default, the HTTP service doesn’t start automatically, whereas in Ubuntu, the Apache2 service does.
Vagrant Clean Up
To destroy all VMs:
$ exit
$ vagrant destroy --force
$ cd ../centos
$ vagrant destroy --force
$ vagrant global-status --prune
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!