The Vagrant provision will help you to automate the installing the software, configurations and commands in your Vagrant Boxes. Vagrant gives us lots of provisioner options. In this post, we will show some vagrant shell provisioner examples.
Before going to explain more on Vagrant Shell provisioner, we suggest you to read our this post to understand Vagrantfile once.
Syntax: To define vagrant shell provision in Vagrantfile, this is the syntax.Block_Name.vm.provision "shell",
There are two major options of Vagrant Shell Provisioner
a. inline
: Specifies a shell command inline to execute on the remote machine
b. path
: Path to a shell script to upload and execute. Even the shell script can be located remotely and should be accessible.
Examples Of Inline Option Of Shell Provisioner
All given below example, we are using in “config” block. If you want to use in other block which you have defined in your Vagrantifle, you can easily do it. In short, it is not constraint to “config” block only.
Example 1 (Inline): In this example, we have simply written the shell commands.
Vagrant.configure("2") do |config| config.vm.provision "shell", inline: "touch /tmp/test ; mkdir ~/testDir" end
Example 2 (Inline): In this example, we are touching some ruby style because Vagrant is ruby based. Then we have called the $script in inline.
$script = <<-SCRIPT echo "Hello World". touch /tmp/testfile SCRIPT Vagrant.configure("2") do |config| config.vm.provision "shell", inline: $script end
Example 3 (Inline): In this one, when your script already exist in Guest Operating System then you have to just mention the path of script.
Vagrant.configure("2") do |config| config.vm.provision "shell", inline: "/bin/sh /tmp/script.sh" end
Examples Of Path Option Of Shell Provisioner
In ‘path’, we generally have external script. The external script could be either in host machine (from where you run vagrant command) or located in accessible remote location.
Example 1 (Path): In this example, the external script is in host machine. So it will be uploaded from host to guest machine and then it will execute.
Vagrant.configure("2") do |config| config.vm.provision "shell", path: "script.sh" end
Example 2 (Path): In this example, the script is in remote location. Always remember the external script should be accessible from your system’s network.
Vagrant.configure("2") do |config| config.vm.provision "shell", path: "https://example.com/script.sh" end
Scenario: Install Jenkins through Vagrant Shell Provisioner in CentOS 8
In this scenario, we will install the Jenkins by using Vagrant Shell provisioner. The environment will be setup in a Single VirtualBox VM and remote script we have kept in our Gist. We are using CentOS 8 vagrant box here.
Create a directory in your host system where we will keep Vagrantfile.
mkdir -p /home/sharad/vagrant_boxes/Single_VM/jenkins
Change to newly created directory
cd /home/sharad/vagrant_boxes/Single_VM/jenkins
Create a Vagrantfile there and paste the given below content. You can find our gist script in shell provisioner section.
Vagrant.configure("2") do |config| config.vm.provider "virtualbox" do |vb| vb.name = "jenkins-server-01" vb.memory = 1024 vb.cpus = 2 end config.vm.box = "centos/8" config.vm.hostname = "jenkins01" config.vm.network "private_network", ip: "192.168.33.10" config.vm.provision "shell", path: "https://gist.githubusercontent.com/sharadchhetri/cfe3c76791b41b0f245f43dd127b0810/raw/947d9d95159866af0cc1c9fa8d0249c66546a6a8/install-jenkins-centos.sh" end
Save the file and spin up new Virtual Machine with vagrant
vagrant up
Now wait for some minutes. On terminal you can see the commands are executing. The Jenkins server will be start listening on port no. 8080. As per our Vagrantfile, we have defined the private network ip as 192.168.33.10 .
Jenkins setup is still left. To complete that open the web browser hit the url http://192.168.33.10:8080
. Now from web browser you can do the rest of the setup.
To ssh this vagrant jenkins machine use the command vagrant ssh
.
When and how to use Vagrant Provision
1. When provision is defined in Vagrantfile and on doing vagrant up
the defined provision will be executed.
2. When your Vagrant environment already running and you want to run the provision in runtime then hit the command vagrant provision
3. When you want to execute the provision and reload the Vagrant environment then here is the command – vagrant reload --provision
4. When you do not want to execute provision then use --no-provision
option along with the vagrant up
and vagrant reload
command.
Example: vagrant up --no-provision