Skip to content

Instantly share code, notes, and snippets.

@xijo
Created July 13, 2015 20:20
Show Gist options
  • Save xijo/13ec5b8b624318022aee to your computer and use it in GitHub Desktop.
Save xijo/13ec5b8b624318022aee to your computer and use it in GitHub Desktop.
Vagrant file, ready for local ansible development
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
remote_user = ENV['ANSIBLE_REMOTE_USER']
raise "You need to set ANSIBLE_REMOTE_USER in your shell" unless remote_user
# Add user #{remote_user} with group wheel
$create_user = <<SCRIPT
useradd -m -s /bin/bash -U #{remote_user}
usermod -a -G wheel #{remote_user}
SCRIPT
# Enable wheel group in sudoers
$enable_wheel = <<SCRIPT
sed -i -e "s/^# %wheel/%wheel/" /etc/sudoers
SCRIPT
# Set local id_rsa.pub as authorized_key for remote_user
$add_authorized_key = <<SCRIPT
rm -f /home/#{remote_user}/.ssh/authorized_keys
mkdir /home/#{remote_user}/.ssh
cp /home/vagrant/host_pubkey /home/#{remote_user}/.ssh/authorized_keys
chown #{remote_user}:#{remote_user} /home/#{remote_user}/.ssh/authorized_keys
chmod 600 /home/#{remote_user}/.ssh/authorized_keys
SCRIPT
# Remove local id_rsa.pub from image
$remove_host_pubkey = <<SCRIPT
rm -f /home/vagrant/host_pubkey
echo "id_rsa.pub installed for user #{remote_user}"
SCRIPT
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
config.vm.define "vagrant31" do |box|
box.vm.box = "chef/centos-6.6"
box.vm.network :private_network, ip: '192.168.42.31'
end
config.vm.define "vagrant32" do |box|
box.vm.box = "chef/centos-6.6"
box.vm.network :private_network, ip: '192.168.42.32'
end
config.vm.define "vagrant33" do |box|
box.vm.box = "chef/centos-6.6"
box.vm.network :private_network, ip: '192.168.42.33'
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
end
end
config.vm.provision 'file',
run: 'once',
source: '~/.ssh/id_rsa.pub',
destination: '/home/vagrant/host_pubkey'
config.vm.provision 'shell', privileged: true, run: 'once', inline: $create_user
config.vm.provision 'shell', privileged: true, run: 'once', inline: $enable_wheel
config.vm.provision 'shell', privileged: true, run: 'once', inline: $add_authorized_key
config.vm.provision 'shell', privileged: true, run: 'once', inline: $remove_host_pubkey
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment