Skip to content

Instantly share code, notes, and snippets.

@vStone vStone/Vagrantfile Secret
Created Dec 6, 2012

Embed
What would you like to do?
An (improved) Vagrantfile
#!/bin/bash
#
environment=${1-develop}
boxname=${2-unknown}
sources=${3-puppet}
## Do your puppet stuff here.
echo "YOU MUST CUSTOMIZE THE $0 script!!"
exit 0;
##### This is what I use. Not sure this will work for you or if it even works
## Puppet setup.
[ -d /etc/puppet/environments/${environment} ] || mkdir -pv /etc/puppet/environments/${environment}
[ -d /vagrant/graphs/${boxname} ] || mkdir -pv /vagrant/graphs/${boxname}
[ -f /vagrant/${sources}/puppet.conf-base ] && cp -v /vagrant/${sources}/puppet.conf-base /etc/puppet/puppet.conf
# Sync puppet code to the right environment.
rsync -alrcWt --del --progress \
--exclude=.git --exclude=.svn --exclude=*.swp \
--exclude=vendor/ --exclude=.vendor/ \
/vagrant/${sources}/* /etc/puppet/environments/${environment}/
## Hiera setup
[ -f /vagrant/${sources}/hiera.yaml ] && cp -v /vagrant/${sources}/hiera.yaml /etc/puppet/hiera.yaml || \
( [ -f /vagrant/${sources}/hiera.yaml-base ] && cp -v /vagrant/${sources}/hiera.yaml-base /etc/puppet/hiera.yaml )
hiera_sync_to=/etc/puppet/hieradata
## If we include the environment in the datadir setting, add it to the path.
# Only works where the exact path is /etc/puppet/hieradata/environment ofcourse.
[ -f /etc/puppet/hiera.yaml ] && grep -q ':datadir:.*%{environment}' /etc/puppet/hiera.yaml && hiera_sync_to=/etc/puppet/hieradata/${environment}
[ -d $hiera_sync_to ] || mkdir -pv $hiera_sync_to
## Sync hieradata code.
if [ -d /vagrant/hieradata ]; then
rsync -alrcWt --del --progress \
--exclude=.git --exclude=.svn \
/vagrant/hieradata/ $hiera_sync_to
fi;
## Augeas lenses?
[ -d /var/lib/puppet/lib/augeas/lenses ] || mkdir -pv /var/lib/puppet/lib/augeas/lenses
find /vagrant/${sources} -iname *.aug -exec cp -v {} /var/lib/puppet/lib/augeas/lenses/ \;
## Facts?
[ -d /var/lib/puppet/lib/facter ] || mkdir -pv /var/lib/puppet/lib/facter
find /vagrant/${sources} -iwholename */facter/* -iname *.rb -and -not -iname *spec.rb -exec cp -v {} /var/lib/puppet/lib/facter/ \;
#[ -d /var/lib/puppet/lib/puppet/parser/functions ] || mkdir -pv /var/lib/puppet/lib/puppet/parser/functions
#find /vagrant/${sources} -iwholename "*lib/puppet/parser/functions" -iname *.rb \
# -exec cp -v {} /var/lib/puppet/lib/puppet/parser/functions/ \;
exec puppet apply \
--environment ${environment} --pluginsync \
--verbose --debug --trace --summarize \
--graph --graphdir /vagrant/graphs/${boxname} \
--modulepath '$confdir/environments/$environment/modules/vstone:$confdir/environments/$environment/modules/upstream:$confdir/environments/$environment/modules/dev' \
/etc/puppet/environments/${environment}/manifests/site.pp
cat /var/lib/puppet/state/last_run_summary.yaml
# -*- mode: ruby -*-
# vi: set ft=ruby :
# # Vagrantfile.
#
# Some noteworthy 'features'
#
# # scripts
#
# ## pre-scripts
# inside the ./script/ folder, you can have pre-<NAMEOFTHEBOX>.sh scripts which will
# be executed if the script file exists.
#
# ## apply scripts (DEFAULT BEHAVIOUR)
# if you did not specify :puppet => :client, a script called puppetrun-<NAMEOFTHEBOX>.sh will be executed.
# with a fallback to puppetrun.sh
#
#
domain = "example.com"
puppetmaster = "puppetmaster.#{domain}"
## Used plugins:
# * vagrant-hostmanager (1.5.0)
VIRTUAL_MACHINES = {
:puppetmaster => {
:ip => '192.168.100.20', ## Private network
:hostname => "puppetmaster01.#{domain}",
:hostaliases => [puppetmaster, "puppetdb.#{domain}"],
:sourcedir => 'puppet', ## Folder which contains the tree.
},
:client => {
:puppet => :client,
:ip => '192.168.100.30',
:hostname => "client01.#{domain}",
},
:apply => {
:ip => '192.168.100.60',
:hostname => "apply01.#{domain}",
:sourcedir => 'puppet',
},
:box => {
:hostname => "box01.#{domain}",
:box => 'custom-box-name',
:box_url => 'http://some_box_url',
}
}
Vagrant.configure('2') do |config|
### Global configuration for ALL boxes...
config.vm.box = 'centos6'
# we use the hostmanager provisioner.
config.hostmanager.enabled = false
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
VIRTUAL_MACHINES.each do |name,cfg|
config.vm.define name do |vm_config|
## Some additional attributes I use in my scripts. See below.
sourcedir = cfg[:sourcedir] || 'puppet'
environment = cfg[:environment] || 'develop'
graphdir = "graphs/#{name}"
## Configure basics.
vm_config.vm.box = cfg[:box] if cfg[:box]
vm_config.vm.box_url = cfg[:box_url] if cfg[:box_url]
vm_config.vm.hostname = cfg[:hostname] if cfg[:hostname]
vm_config.hostmanager.aliases = cfg[:hostaliases] if cfg[:hostaliases]
vm_config.vm.network :private_network, ip: cfg[:ip] if cfg[:ip]
if cfg[:forwards]
cfg[:forwards].each do |guest, host|
vm_config.vm.network :forwarded_port, guest: guest, host: host
end
end
# Creates the directory for puppet runs using --graph.
unless Dir.exists?(File.expand_path(File.join(File.dirname(__FILE__), "./#{graphdir}")))
Dir.mkdir(File.expand_path(File.join(File.dirname(__FILE__), "./#{graphdir}")))
end
## Update hosts file on the machine.
vm_config.vm.provision :hostmanager
## Run scripts that match pre-<nameofthebox>.sh
if File.exists?(File.expand_path(File.join(File.dirname(__FILE__), "./scripts/pre-#{name}.sh")))
vm_config.vm.provision :shell do |shell|
shell.path = File.expand_path(File.join(File.dirname(__FILE__), "./scripts/pre-#{name}.sh"))
shell.args = "#{environment} #{sourcedir}"
end
end
## If :puppet => :client, do a puppet run against the master
if cfg[:puppet] and cfg[:puppet] == :client
vm_config.vm.provision :puppet_server do |puppet|
puppet.puppet_server = puppetmaster
puppet.options = "--verbose --debug --environment #{environment} --test --trace --graph --graphdir #{graphdir}"
end
else
## Run the shell script that matches the name of the box or the default script.
vm_config.vm.provision :shell do |shell|
if File.exists?(File.expand_path(File.join(File.dirname(__FILE__), "./scripts/puppetrun-#{name}.sh")))
shell.path = "scripts/puppetrun-#{name}.sh"
else
shell.path = "scripts/puppetrun.sh"
end
shell.args = "#{environment} #{name} #{sourcedir}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.