• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Learn Vagrant provision with shell provisioner examples

June 7, 2020 by Sharad Chhetri Leave a Comment

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 reloadcommand.

Example:
vagrant up --no-provision

Reference: Vagrant Official Documentation for Provisioner

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. Learn Linux Date Command With Examples
  2. learn very useful alias command with examples
  3. Create single node Virtual Machine with Vagrant
  4. How to customize nologin shell message
  5. Convert hyphen to underscore in between all filenames shell script
  6. How to install korn shell ksh in Linux
  7. Temporarily change the shell after login
  8. shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
  9. Learn tar command and know about its precaution
  10. Install Vagrant on Ubuntu and create first VM

Filed Under: Devops, Linux Tagged With: devops, vagrant

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

How to show line numbers with less command in Linux

sed : find the pattern (keyword) and delete the line from file

Linux: Set user password by using encrypted password in command

Give permission reference to file/dir from other file/dir in linux

Linux Basic Commands For Every Beginner

man command not found in CentOS and Red Hat

How to install libxml2 and libxslt packages on CentOS 6.5

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

Edit the crontab without using crontab -e

configure: error: C++ compiler cannot create executables

master admin password openerp 7.x

How to backup and restore iptables on Linux systems

Error installing rails

How to add/install Ubuntu fonts in CentOS/Red Hat Linux

How to hide php version information in header

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy