Skip to content

Instantly share code, notes, and snippets.

@rduplain
Last active Nov 28, 2019
Embed
What would you like to do?
Vagrantfiles for testing configuration management (Ansible).

Vagrantfiles for testing configuration management (Ansible).

Overview

Configuration management that runs over SSH, namely Ansible, benefits in having test machines available on the network. The enclosed Vagrantfiles provide multiple virtual machines to run as guests on the developer's machine. The guests appear as hosts on the LAN, providing a local ephemeral cloud.

The virtual machines here are similar, but different. One provides Ubuntu 14.04 LTS ("trusty"), another provides Ubuntu 18.04 LTS ("bionic"), and another provides CentOS Linux 8 ("centos8"). Together they provide a test environment for the basis of an inventory to test configuration on machines old and new.

More broadly, these Vagrantfiles demonstrate how to get multiple virtual machines on your LAN for testing from your development machine with vagrant. A simple diff of any two Vagrantfiles suggests how you might template the configuration for larger projects, given enough RAM.

The shell provision in the Vagrantfile provides the starting point as the preexisting state of the virtual machine before configuration management picks up.

Run each Vagrantfile (see Usage below) then connect to throwaway virtual machines on your network.

These Vagrantfiles assist in experimenting with configuration and specific scenarios. To test infrastructure, see Kitchen.

Features

  • Configure each host with 2 CPUs and 1GB RAM.
  • Disable vagrant's default bind/synced folder at /vagrant.
  • Provision with inline shell to set the initial state.
  • Connect to the public network.
  • Append all developer authorized_keys to vagrant SSH.

A Vagrantfile assumes that the machine running vagrant has enough CPUs and RAM to create the virtual machine.

Quickstart

  • Add the ID of your machine's network interface to each Vagrantfile. (See the comment "To display valid options..." in the Vagrantfile.)
  • Set one Vagrantfile per directory; run vagrant in each directory.
  • Bring hosts up with vagrant up && vagrant reload.
  • Stop hosts with vagrant halt.
  • Delete hosts with vagrant destroy.

Preface to Usage

Vagrantfiles for testing only. Run only on trusted networks.

Before you git add . ... These Vagrantfiles are mostly portable, but in configuring the network bridge, they include details about the local developer machine (including use of VirtualBox, which is a developer preference). Therefore, these Vagrantfiles cannot be shared across developer machines and therefore should not be checked into version control as-is without additional consideration.

Usage

Install VirtualBox and vagrant.

Put each Vagrantfile into its own directory:

mkdir -p bionic trusty centos8
mv Vagrantfile.bionic ./bionic/Vagrantfile
mv Vagrantfile.trusty ./trusty/Vagrantfile
mv Vagrantfile.centos8 ./centos8/Vagrantfile

Update each Vagrantfile with the identifier of your network interface. See the comment "To display valid options..." in the Vagrantfile.

Is your LAN configured for DHCP... will the router accept any new host? If so, keep the dhcp configuration already in place. If not, review the public networks documentation and consider setting ip: "192.168.1.99" using whatever static IP value is appropriate.

Maintain a unique, fake MAC address for each Vagrantfile. This is already done in the Vagrantfiles provided, but note that you should adjust the last 6 characters of each mac to a unique value when forking Vagrantfile configurations.

Bring up each host:

cd ./bionic
vagrant up && vagrant reload
cd ./trusty
vagrant up && vagrant reload
cd ./centos8
vagrant up && vagrant reload

You can run these in parallel, but if you changed the Vagrantfile to use the same config.vm.box to make two run the same Ubuntu version, then let vagrant complete one host before starting the other. If there are any errors, review the log output carefully, as the issue is likely with your particular vagrant configuration or the underlying VirtualBox installation.

When you are ready to stop hosts, run vagrant halt in the directory of each Vagrantfile. Run vagrant destroy to delete the virtual machine.

Making changes to the shell provision in the Vagrantfile? With the host running, run:

vagrant provision

Connect to each host:

ssh vagrant@<resulting_bionic_ip>
ssh vagrant@<resulting_trusty_ip>
ssh vagrant@<resulting_centos8_ip>

If your network resolves hostnames on the LAN:

ssh vagrant@bionic
ssh vagrant@trusty
ssh vagrant@centos8

Helpful configuration for ~/.ssh/config:

Host bionic
    User                        vagrant
    StrictHostKeyChecking       no

Host trusty
    User                        vagrant
    StrictHostKeyChecking       no

Host centos8
    User                        vagrant
    StrictHostKeyChecking       no

This allows a more direct ssh invocation and appreciates that vagrant will change host sshd keys on each newly created vagrant up virtual machine.

ssh bionic
ssh trusty
ssh centos8

Now you have hosts on the LAN, accessible by SSH, for testing configuration management.

--@rduplain

# -*- mode: ruby -*-
# vi: set ft=ruby :
# Setup, first time only:
#
# vagrant up && vagrant reload
#
# Usage:
#
# vagrant up
# vagrant ssh
Vagrant.configure(2) do |config|
config.vm.define "bionic"
config.vm.hostname = "bionic"
config.vm.box = "ubuntu/bionic64"
config.vm.box_check_update = false
# Below, bridge value is specific to the host machine interfaces.
# To display valid options:
#
# * Remove bridge key below.
# * vagrant up
# * Review options presented interactively by Vagrant.
# * Ctrl-C, update Vagrantfile.
config.vm.network "public_network",
bridge: "...",
mac: "02CC50F01804",
use_dhcp_assigned_default_route: true
config.vm.provider "virtualbox" do |vb|
vb.cpus = 2
vb.memory = "1024"
vb.name = "bionic"
vb.customize ["modifyvm", :id, "--audio", "none"]
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
config.vm.synced_folder ".", "/vagrant", disabled: true
# Prepare to have all local authorized keys also be authorized by vagrant.
# Append to ~/.ssh/authorized_keys in shell provision below.
config.vm.provision "file",
source: "~/.ssh/authorized_keys",
destination: "~/.ssh/user_authorized_keys"
config.vm.provision "shell",
privileged: true, keep_color: true, inline: <<-SHELL
export LANGUAGE=en_US.UTF-8
export LANG=$LANGUAGE
export LC_ALL=$LANGUAGE
export DEBIAN_FRONTEND=noninteractive
locale-gen $LANGUAGE
update-locale LANGUAGE=$LANGUAGE
update-locale LANG=$LANG
update-locale LC_ALL=$LC_ALL
dpkg-reconfigure locales
timedatectl set-timezone America/New_York
dpkg-reconfigure tzdata
apt-get update
apt-get dist-upgrade -y
apt-get install -y vim
update-alternatives --set editor /usr/bin/vim.basic
echo Adding additional user authorized keys ...
sort -u /home/vagrant/.ssh/{user_authorized_keys,authorized_keys} > \
/tmp/authorized_keys
mv /tmp/authorized_keys /home/vagrant/.ssh/authorized_keys
SHELL
end
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Setup, first time only:
#
# vagrant up && vagrant reload
#
# Usage:
#
# vagrant up
# vagrant ssh
Vagrant.configure(2) do |config|
config.vm.define "centos8"
config.vm.hostname = "centos8"
config.vm.box = "centos/8"
config.vm.box_version = "1905.1"
config.vm.box_check_update = false
# Below, bridge value is specific to the host machine interfaces.
# To display valid options:
#
# * Remove bridge key below.
# * vagrant up
# * Review options presented interactively by Vagrant.
# * Ctrl-C, update Vagrantfile.
config.vm.network "public_network",
bridge: "...",
mac: "02CC50Fce108",
use_dhcp_assigned_default_route: true
config.vm.provider "virtualbox" do |vb|
vb.cpus = 2
vb.memory = "512"
vb.name = "centos8"
vb.customize ["modifyvm", :id, "--audio", "none"]
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
config.vm.synced_folder ".", "/vagrant", disabled: true
# Prepare to have all local authorized keys also be authorized by vagrant.
# Append to ~/.ssh/authorized_keys in shell provision below.
config.vm.provision "file",
source: "~/.ssh/authorized_keys",
destination: "~/.ssh/user_authorized_keys"
config.vm.provision "shell",
privileged: true, keep_color: true, inline: <<-SHELL
export LANGUAGE=en_US.UTF-8
export LANG=$LANGUAGE
export LC_ALL=$LANGUAGE
localectl set-locale LANG=$LANG
timedatectl set-timezone America/New_York
timedatectl status
yum update -y
yum install -y vim
echo Adding additional user authorized keys ...
sort -u /home/vagrant/.ssh/{user_authorized_keys,authorized_keys} > \
/tmp/authorized_keys
mv /tmp/authorized_keys /home/vagrant/.ssh/authorized_keys
SHELL
end
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Setup, first time only:
#
# vagrant up && vagrant reload
#
# Usage:
#
# vagrant up
# vagrant ssh
Vagrant.configure(2) do |config|
config.vm.define "trusty"
config.vm.hostname = "trusty"
config.vm.box = "ubuntu/trusty64"
config.vm.box_check_update = false
# Below, bridge value is specific to the host machine interfaces.
# To display valid options:
#
# * Remove bridge key below.
# * vagrant up
# * Review options presented interactively by Vagrant.
# * Ctrl-C, update Vagrantfile.
config.vm.network "public_network",
bridge: "...",
mac: "02CC50F01404",
use_dhcp_assigned_default_route: true
config.vm.provider "virtualbox" do |vb|
vb.cpus = 2
vb.memory = "1024"
vb.name = "trusty"
vb.customize ["modifyvm", :id, "--audio", "none"]
vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
end
config.vm.synced_folder ".", "/vagrant", disabled: true
# Prepare to have all local authorized keys also be authorized by vagrant.
# Append to ~/.ssh/authorized_keys in shell provision below.
config.vm.provision "file",
source: "~/.ssh/authorized_keys",
destination: "~/.ssh/user_authorized_keys"
config.vm.provision "shell",
privileged: true, keep_color: true, inline: <<-SHELL
export LANGUAGE=en_US.UTF-8
export LANG=$LANGUAGE
export LC_ALL=$LANGUAGE
export DEBIAN_FRONTEND=noninteractive
locale-gen $LANGUAGE
update-locale LANGUAGE=$LANGUAGE
update-locale LANG=$LANG
update-locale LC_ALL=$LC_ALL
dpkg-reconfigure locales
timedatectl set-timezone America/New_York
dpkg-reconfigure tzdata
apt-get update
apt-get dist-upgrade -y
apt-get install -y vim
update-alternatives --set editor /usr/bin/vim.basic
echo Adding additional user authorized keys ...
sort -u /home/vagrant/.ssh/{user_authorized_keys,authorized_keys} > \
/tmp/authorized_keys
mv /tmp/authorized_keys /home/vagrant/.ssh/authorized_keys
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment