Do you like Vagrant? … Yes? … Then you will love Vagrant Manger! Vagrant Manager is free and available for Mac OS and Windows. But pictures say more than 1000 words…
This time shows the tutorial two topics in one. The first topic is: “How an easy to configure SSH host jump”. The 2nd topic is: “Provisioning examples for Vagrant”.
The title says it, … this tutorial is about Packer, CentOS 7 and Vagrant. After that, you should be able to integrate the creation of Vagrant base boxes into your Build-server. There is on small exception to other – the VirtualBox Guest Additions will be provided via PlugIn! Because other users could may have different versions.
install
cdrom
lang en_US.UTF-8
keyboard us
timezone UTC
network --bootproto=dhcp
firewall --disabled
rootpw --plaintext packer
user --name=vagrant --password=vagrant
auth --enableshadow --passalgo=sha512 --kickstart
selinux --permissive
text
skipx
clearpart --all --initlabel
zerombr
autopart
bootloader --location=mbr
firstboot --disable
reboot
%packages --instLangs=en_US.utf8 --nobase --ignoremissing --excludedocs
@^minimal
@core
-aic94xx-firmware
-atmel-firmware
-b43-openfwwf
-bfa-firmware
-ipw2100-firmware
-ipw2200-firmware
-ivtv-firmware
-iwl100-firmware
-iwl105-firmware
-iwl135-firmware
-iwl1000-firmware
-iwl2000-firmware
-iwl2030-firmware
-iwl3160-firmware
-iwl3945-firmware
-iwl4965-firmware
-iwl5000-firmware
-iwl5150-firmware
-iwl6000-firmware
-iwl6000g2a-firmware
-iwl6000g2b-firmware
-iwl6050-firmware
-iwl7260-firmware
-libertas-usb8388-firmware
-ql2100-firmware
-ql2200-firmware
-ql23xx-firmware
-ql2400-firmware
-ql2500-firmware
-rt61pci-firmware
-rt73usb-firmware
-xorg-x11-drv-ati-firmware
-zd1211-firmware
%end
%post --log=/root/ks.log
SEE NEXT PICTURE!!!! The security settings of my provider does not allow this content!
%end
# -*- mode: ruby -*-
Vagrant.require_version ">= 1.8.1"
Vagrant.configure("2") do |config|
config.vm.box = "packer/centos7"
config.vm.box_url = "target/virtualbox-CentOS-7.box"
config.vm.synced_folder ".", "/vagrant", disabled: true
config.vm.provider "virtualbox" do |vb|
vb.name = "CentOS-7"
vb.cpus = "2"
vb.memory = "2048"
vb.gui = false
end
end
Usage
# run packer build (via make)
$ make build
# run vagrant up (via make)
$ make run
# run vagrant reload (via make)
$ make reload
# run vagrant ssh (via make)
$ make ssh
# destroy everything (via make)
$ make clean
Depending on the vagrant version and how you build/package boxes, you may know the Authentication failure after command “$ vagrant up“. Here now two simple tips, how to prevent or fix it.
Prevention
If you build on an existing box you should not allow Vagrant to automatically insert a new key pair.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure('2') do |config|
config.ssh.insert_key = false
...
end
Workaround
If the problem already exists…
# start VM (local)
$ vagrant up
...
default: Warning: Authentication failure. Retrying...
Timed out while waiting for the machine to boot...
...
# login via password authentication (local -> VM)
$ vagrant ssh
# start edit authorized_keys (VM)
[vagrant@acme ~]$ vim .ssh/authorized_keys
# add latest key from https://github.com/mitchellh/vagrant/blob/master/keys/vagrant.pub and save
# leave vm (VM -> local)
$ exit
# stop VM (local)
$ vagrant halt
On command “$ vagrant halt” Vagrant will automatically insert a new keypair.
You may need to watch different log files on automated test runs. With log.io you can simply monitoring log files via browser! This tutorial shows how easy it is.
This time again some topics in one tutorial. We touch Vagrant with Shell provision (external script), Docker Remote API and something CentOS 7 configuration.
# start VM via Vagrant
$ vagrant up
# SSH into VM via Vagrant
$ vagrant ssh
# check if API is enabled (optional)
$ docker -H=tcp://127.0.0.1:4243 images
Now we edit docker.service file. The path may be different!
# find docker.service file
$ systemctl status docker
# edit docker.service
$ vim /usr/lib/systemd/system/docker.service
...
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// -D -H tcp://127.0.0.1:4243
...
# reload and restart docker service
$ systemctl daemon-reload && systemctl restart docker
# test docker API again
$ docker -H=tcp://127.0.0.1:4243 version
Note: Be careful if you use tcp://0.0.0.0:4243 !!!!
# some tests with curl and jq on Docker Remote API
$ curl -X GET http://127.0.0.1:4243/version | jq .
$ curl -X GET http://127.0.0.1:4243/info | jq .DriverStatus
$ curl -X GET http://127.0.0.1:4243/networks | jq '.[0]'
Read the manual of jq, the power of curl and jq together is awesome.
Create a simple Vagrant BaseBox (as here) and if you like use the following Vagrantfile.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "lupin/centos7"
# config.vm.network "public_network"
config.vm.synced_folder ".", "/vagrant", disabled: true
# config.vm.synced_folder "./shared", "/vagrant"
config.vm.provider "virtualbox" do |vb|
vb.cpus = "2"
vb.memory = "2048"
end
if Vagrant.has_plugin?("vagrant-hostsupdater")
config.vm.network :private_network, ip: "10.0.0.10"
config.hostsupdater.aliases = ["example.test"]
end
config.vm.provision "shell", inline: <<-SHELL
sudo yum update -y
sudo yum install -y vim docker
sudo yum clean all
sudo systemctl enable docker
sudo systemctl start docker
sudo setenforce 0
SHELL
config.vm.provision "docker" do |d|
d.run "nginx", args: "-p 8080:80"
end
config.vm.provision "shell", inline: "sudo hostname -I"
end
1. vagrant-camera
# install camera plugin
$ vagrant plugin install vagrant-camera
# start VM via Vagrant
$ vagrant up
# do screenshot on current target folder
$ vagrant camera -s .
# show all pictures (Mac OS X)
$ open screenshot_default_*.png
2. vagrant-vbguest
# start VM via Vagrant
$ vagrant up
# ssh into VM (optional)
$ vagrant ssh
# check current version (optional)
$ VBoxControl --version
...
5.0.20r106931
# exit from VM (optional)
$ exit
# install vbguest plugin
$ vagrant plugin install vagrant-vbguest
# reload VM
$ vagrant reload
# ssh into VM (optional)
$ vagrant ssh
# list all downloaded (optional)
$ ls -l /opt
...
VBoxGuestAdditions-5.0.14
VBoxGuestAdditions-5.0.20
3. vagrant-hostsupdater
# install hostsupdater plugin
$ vagrant plugin install vagrant-hostsupdater
# check content of local hosts (optional)
$ cd /etc && cat hosts
# start VM
$ vagrant up
# or reload if already running
$ vagrant reload --provision
# check content of local hosts (optional)
$ cd /etc && cat hosts
# check status
$ vagrant ssh -c 'curl -v example.test:8080'
# stop VM
$ vagrant halt
# check content of local hosts (optional)
$ cd /etc && cat hosts
Ruby`s stdlib provides an YAML module for data serialization. By using this module, we can create only one Vagrantfile which reads the configuration from YAML files. If you are thinking of build servers – so only YAML files are needed for generating complete environments.