Created
February 1, 2017 22:05
-
-
Save mohclips/d4c0edf665f47ed59338c6f7c4a18454 to your computer and use it in GitHub Desktop.
A Vagrantfile to create 4x ubuntu instances for Docker Swarm and 1x centos Ansible master
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 | |
$ansible_script = <<SHELL | |
# 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 | |
SHELL | |
$redhat_network = <<SHELL | |
SHELL | |
servers = { | |
"ansible01" => { :ip => "172.30.5.60", :bridge => "eth1", :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => CENTOS_BOX, :script => $ansible_script }, | |
"docker-engine01" => { :ip => "172.30.5.61", :bridge => "eth1", :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX, :script => '' }, | |
"docker-engine02" => { :ip => "172.30.5.62", :bridge => "eth1", :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX, :script => '' }, | |
"docker-engine03" => { :ip => "172.30.5.63", :bridge => "eth1", :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX, :script => '' }, | |
"docker-engine04" => { :ip => "172.30.5.64", :bridge => "eth1", :mem => DOCKER_MEM, :cpus => DOCKER_CPUS, :box => UBUNTU_BOX, :script => '' } | |
} | |
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_with_index do |(hostname, info), index| | |
# | |
# build a vm - from the server dict | |
# | |
config.vm.define hostname do |cfg| | |
cfg.vm.box = info[:box] | |
cfg.vm.hostname = hostname | |
# note the public network | |
cfg.vm.network "public_network", ip: info[:ip], bridge: info[:bridge] | |
config.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 - but only once | |
# | |
config.vm.provision "shell", run: "once" do |s| | |
ssh_prv_key = "" | |
ssh_pub_key = "" | |
if File.file?("#{Dir.home}/.ssh/vagrant") | |
ssh_prv_key = File.read("#{Dir.home}/.ssh/vagrant") | |
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/vagrant.pub").first.strip | |
else | |
puts "No SSH key found. You will need to remedy this before pushing to the repository." | |
end | |
puts "SSH key insertion" | |
s.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 | |
# | |
# this fixes the centos boxes not enabling their eth1 IPs | |
# | |
$box = info[:box] | |
if $box =~ /redhat|centos/i | |
puts "RedHat check and network fix" | |
s.inline = <<SHELL | |
if [ -e /etc/redhat-release ] ; then | |
echo "Redhat release found" | |
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 | |
# | |
# any script per vm to install | |
# | |
s.inline = info[:script] | |
# end scripts | |
end | |
end | |
end | |
################################################################################ | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment