• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
sharadchhetri.com

sharadchhetri.com

Linux,Cloud Computing And DevOps

  • Home
  • Linux
  • CloudComputing
    • Introduction Cloud Computing
    • Amazon AWS
  • Free E-Books
  • About Me

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

Do'nt be greedy, share the knowledge!

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

Filed Under: 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

Follow Us

  • Facebook
  • Twitter
  • GitHub
  • YouTube

Recent Posts

  • Activate.ps1 cannot be loaded because running scripts is disabled on this system
  • How to install Java (OpenJDK) on Ubuntu Linux
  • How to install Groovy on Ubuntu 20.04 LTS
  • How to Disable selinux in Red Hat or CentOS
  • How to remove date from WordPress Post URL

Top Posts & Pages

  • How to delete mail queue in Postfix
  • How to fix read only USB pen drive in Ubuntu
  • How to start / stop / restart / reload iptables on CentOS 7 / RHEL 7
  • 4 different commands to check the load average in linux
  • How to set hostname and FQDN on CentOS 7 and RHEL 7
  • How to find absolute path of command on Linux / Unix : which command
  • How to create Jenkins user by command line and GUI
  • How to print particular line number by using sed command
  • How to setup Jenkins Credentials for Git repo access
  • make command not found in linux CentOS Red Hat ubuntu Debian

DevOps Posts

  • vagrant cloud flow

    Install Vagrant on Ubuntu and create first VM

  • git jenkins credential github

    How to setup Jenkins Credentials for Git repo access

  • terraform

    how to install terraform in Linux : CentOS-Ubuntu

  • docker image

    How to create Ubuntu docker base image

  • Install Ansible on Ubuntu / CentOS / RHEL

Footer

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Sharad Chhetri is an experienced Linux - Cloud Engineer & freelancer. Working on Open Source Technology since RHEL 4.0 (Red Hat Enterprise Linux). He loves sharing the knowledge which earned from real scenarios. Don't be surprised if you find him in technology seminars and meetup groups. You can contact him on email for freelance projects at admin@sharadchhetri.com. Read More…

Tags

Amazon AWS apache automation awk bash script CentOS centos 7 debian devops docker freebsd ftp ghost git grep hostname jenkins kvm linux linux command linux commands MariaDB Server mysql Nagios nginx Octopress owncloud Owncloud 6 php postfix postgres python Red Hat rpm sed selinux ssh swap ubuntu user management vagrant varnish virtualbox vsftp wordpress

Recent Comments

  • Sharad Chhetri on How to fix read only USB pen drive in Ubuntu
  • iain mckeand on How to fix read only USB pen drive in Ubuntu
  • Sharad Chhetri on Secondary Logging : save all users history command output as log
  • Sharad Chhetri on How to fix read only USB pen drive in Ubuntu
  • er on What is /dev/shm and how to mount /dev/shm
  • Bala on Send nagios report as pdf file via email
  • Terry on How to fix read only USB pen drive in Ubuntu
  • Terry on How to fix read only USB pen drive in Ubuntu

Copyright © 2009 - 2022 · All Rights Reserved sharadchhetri.com · · Privacy Policy ·
· sitemap.xml · ·The content is copyrighted to sharadchhetri.com and may not be reproduced on other websites without our permission. ·

Copyright © 2022 · Genesis Sample on Genesis Framework · WordPress · Log in