Skip to content

Instantly share code, notes, and snippets.

@mohclips
Created February 6, 2017 21:56
Show Gist options
  • Save mohclips/8720c66f7c5af9bea2d9646cb3808ea8 to your computer and use it in GitHub Desktop.
Save mohclips/8720c66f7c5af9bea2d9646cb3808ea8 to your computer and use it in GitHub Desktop.
Vagrantfile to launch multiple instances with specified provisioning scripts
echo "**** ANSIBLE INSTALLER ****"
# do this to refresh your patch repo db
sudo yum makecache fast
sudo yum update -y
# install stuff
sudo yum install vim epel-release yum-utils git -y
# as Ansible comes from EPEL, we install it after we installed EPEL
sudo yum install ansible python-pip gcc python-devel openssl-devel -y
# now install winrm to access windows boxes from Ansible
sudo pip install pywinrm
# fix for missing/outdated security libs
sudo pip install requests[security]
# openstack clients
sudo pip install python-openstackclient
echo "**** DOCKER INSTALLER ****"
echo "Use ansible to install"
exit 0
# -*- mode: ruby -*-
# vi: set ft=ruby :
# vim: noai:ts=2:sw=2:et
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
UBUNTU_BOX='ubuntu/trusty64'
CENTOS_BOX='centos/7'
DOCKER_MEM = 1024
DOCKER_CPUS = 2
# to find our provisioning scripts
dir = Dir.pwd
vagrant_dir = File.expand_path(File.dirname(__FILE__))
servers = {
"ansible01" => { :ip => "172.30.5.60", :bridge => "eth1",
:mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => CENTOS_BOX,
:scripts => "provision/ansible-install.sh" },
"docker-engine01" => { :ip => "172.30.5.61", :bridge => "eth1",
:mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
:scripts => "provision/docker-install.sh" },
"docker-engine02" => { :ip => "172.30.5.62", :bridge => "eth1",
:mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
:scripts => "provision/docker-install.sh" },
"docker-engine03" => { :ip => "172.30.5.63", :bridge => "eth1",
:mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
:scripts => "provision/docker-install.sh" },
"docker-engine04" => { :ip => "172.30.5.64", :bridge => "eth1",
:mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX,
:scripts => "provision/docker-install.sh" }
}
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box_check_update = false
# enable logging in via ssh with a password
#config.ssh.username = "vagrant"
#config.ssh.password = "vagrant"
################################################################################
servers.each do |hostname, info|
#
# build a vm - from the server dict
#
config.vm.define hostname do |cfg| # a define per hostname
cfg.vm.box = info[:box]
cfg.vm.hostname = hostname
# note the public network
cfg.vm.network "public_network", ip: info[:ip], bridge: info[:bridge]
cfg.vm.provider "virtualbox" do |v|
v.name = hostname
v.memory = info[:mem]
v.cpus = info[:cpus]
v.customize ["modifyvm", :id, "--hwvirtex", "on"]
end
#
# do some provisioning
#
ssh_prv_key = ""
ssh_pub_key = ""
if not File.file?("#{Dir.home}/.ssh/vagrant")
puts "No SSH key found. You will need to remedy this before pushing to the repository."
else
ssh_prv_key = File.read("#{Dir.home}/.ssh/vagrant")
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/vagrant.pub").first.strip
cfg.vm.provision "shell", inline: <<-SHELL
if grep -sq "#{ssh_pub_key}" /home/vagrant/.ssh/authorized_keys; then
echo "SSH keys already provisioned."
exit 0;
fi
echo "SSH key provisioning."
mkdir -p /home/vagrant/.ssh/
touch /home/vagrant/.ssh/authorized_keys
echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
echo #{ssh_pub_key} > /home/vagrant/.ssh/id_rsa.pub
chmod 644 /home/vagrant/.ssh/id_rsa.pub
echo "#{ssh_prv_key}" > /home/vagrant/.ssh/id_rsa
chmod 600 /home/vagrant/.ssh/id_rsa
chown -R vagrant:vagrant /home/vagrant
exit 0
SHELL
end
#
# this fixes the centos boxes not enabling their eth1 IPs
#
puts "box #{info[:box]} #{hostname}"
if info[:box] =~ /redhat|centos/i
puts "RedHat check and network fix - #{info[:box]}"
cfg.vm.provision "shell", inline: <<-SHELL
if [ -e /etc/redhat-release ] ; then
echo "Redhat release found on #{info[:box]}"
if [ $(ip a s dev eth1 | grep -c "inet #{info[:ip]}") -eq 0 ] ; then
echo "Restarting network"
touch /tmp/network-setup
sudo nmcli connection reload
sudo systemctl restart network.service
else
echo "Network is already up"
fi
else
echo "Not redhat"
fi
exit 0
SHELL
end
#
# per box provisioning - by hostname
#
provision_filename = hostname + "-provision.sh"
cfg.vm.provision "shell", inline: "echo #{provision_filename}"
if File.exists?(File.join(vagrant_dir,'provision',hostname + "-provision.sh")) then
cfg.vm.provision "shell", inline: "echo +++exists+++"
cfg.vm.provision "shell", :path => File.join( "provision", hostname + "-provision.sh" )
else
cfg.vm.provision "shell", inline: "echo PROVISION FILE DOES NOT EXIST!"
end
end # config.vm.define hostname
end # servers.each
################################################################################
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment