Vagrant Provisioning

Using Shell Provisioner to Configure Virtual Systems

Prerequisites

Installing Vagrant on macOS (using Homebrew)

Installing Vagrant on Windows (using Chocolatey)

Installing Vagrant on Fedora

Part I: Provisioning Ubuntu Systems

Ubuntu 16.04 Xenial Xerus

Vagrant Structure

WORKAREA=${WORKAREA:-"${HOME}/vagrant-shell"}mkdir -p ${WORKAREA}/scripts
touch ${WORKAREA}/{Vagrantfile,scripts/hello_web.sh}
cd ${WORKAREA}
~/vagrant-shell
├── Vagrantfile
└── scripts
└── hello_web.sh

Vagrant Configuration

script_path = './scripts'Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "forwarded_port", guest: 80, host: 8080
####### Provision #######
config.vm.provision "shell" do |script|
script.path = "#{script_path}/hello_web.sh"
script.args = %w(apache2 apache2 /var/www/html)
end
end
vagrant up --no-provision

Provisioning Script

#!/usr/bin/env bash#### Set variables with intelligent defaults
APACHE_PACKAGE=${1:-'apache2'}
APACHE_SERVICE=${2:-'apache2'}
APACHE_DOCROOT=${3:-'/var/www/html'}
#### Download and Install Package
apt-get update
apt-get install -y ${APACHE_PACKAGE}
#### Start, Enable Service
systemctl start ${APACHE_SERVICE}.service
systemctl enable ${APACHE_SERVICE}.service
#### Create Content
cat <<-'HTML' > ${APACHE_DOCROOT}/index.html
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
HTML
vagrant provision

Testing the Solution

curl -i http://127.0.0.1:8080
HTTP/1.1 200 OK
Date: Wed, 08 Aug 2018 08:41:30 GMT
Server: Apache/2.4.18 (Ubuntu)
Last-Modified: Wed, 08 Aug 2018 08:39:09 GMT
ETag: "37-572e871b6e929"
Accept-Ranges: bytes
Content-Length: 55
Content-Type: text/html
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>

Part II: Adapting to Other Operating Systems

Debian • Ubuntu • CentOS • Fedora • Gentoo • ArchLinux

Update Vagrantfile

script_path = './scripts'Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 8080
####### Provision #######
config.vm.provision "shell" do |script|
script.path = "#{script_path}/hello_web.sh"
script.args = %w(
httpd
httpd
/var/www/html
)
end
end
Vagrant box name and arguments for package, service, docroot
script_path = './scripts'Vagrant.configure("2") do |config|
config.vm.box = "generic/gentoo"
config.vm.network "forwarded_port", guest: 80, host: 8080
####### Provision #######
config.vm.provision "shell" do |script|
script.path = "#{script_path}/hello_web.sh"
script.args = %w(
www-servers/apache
apache2
/var/www/localhost/htdocs
)
end
end

Updating Provisioning Script

#!/usr/bin/env bash#### Set variables with intelligent defaults
APACHE_PACKAGE=${1:-'apache2'}
APACHE_SERVICE=${2:-'apache2'}
APACHE_DOCROOT=${3:-'/var/www/html'}
#### Download and Install Package
DISTRO=$(
awk -F= '/^ID=/{print tolower($2) }' /etc/os-release \
| tr -d '"'
)
case "${DISTRO}" in
centos|rhel)
yum install -y ${APACHE_PACKAGE}
;;
fedora)
dnf install -y ${APACHE_PACKAGE}
;;
debian|ubuntu)
apt-get update
apt-get install -y ${APACHE_PACKAGE}
;;
gentoo)
emerge ${APACHE_PACKAGE}
;;
arch)
pacman -Syu --noconfirm
pacman -S --noconfirm ${APACHE_PACKAGE}
;;
*)
echo "Distro '${DISTRO}' not supported" 2>&1
exit 1
;;
esac
#### Start, Enable Service
case "${DISTRO}" in
centos|rhel|fedora|debian|ubuntu|arch)
systemctl start ${APACHE_SERVICE}.service
systemctl enable ${APACHE_SERVICE}.service
;;
gentoo)
rc-update add ${APACHE_SERVICE} default
/etc/init.d/${APACHE_SERVICE} start
;;
*)
echo "Distro '${DISTRO}' not supported" 2>&1
exit 1
;;
esac
#### Create Content
cat <<-'HTML' > ${APACHE_DOCROOT}/index.html
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
HTML

Shell Provisioning is Complex — Don’t do it

package($package_name).install()
service($service_name).enable().start()
file("$docroot/index.html", $source).create()

Wrapping Up

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store