Skip to content
This repository has been archived by the owner on Dec 2, 2018. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
blalor committed Jan 29, 2013
0 parents commit 0aceb65
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vagrant
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Reusable Puppet config for Vagrant

This provides a very simple example that shows how you can reuse
[Puppet][puppet] configuration between [Vagrant][vagrant] and your non-Vagrant
systems, including data lookup via Hiera.

Tested with Puppet 3.0.2.

## Vagrant

The Vagrant-specific configuration is captured in the `Vagrantfile`. The
resulting Puppet invocation is

cd /tmp/vagrant-puppet/manifests \
&& FACTER_is_vagrant='true' puppet apply \
--hiera_config hiera.yaml \
--modulepath '/tmp/vagrant-puppet/modules-0' \
/tmp/vagrant-puppet/manifests/default.pp

When provisioned (via `vagrant up` or `vagrant provision`) you will see the
following on the console:

Notice: role::ui configured for vagrant; hostname: 192.168.42.42

## Other environments

A sample `puppet_apply.sh` script is included in the `puppet` directory. All
that is required is to clone this repository (or otherwise make the files
available on the target system).

## Tweaks

### `is_vagrant` fact

This could be replaced with the direct definition of a `data_center` fact.

### Vagrant shell provisioner

Vagrant takes care of providing mounts for the manifests and modules, so the
configuration would only get shuffled around a bit, but it's possible to
directly invoke `puppet_apply.sh` from Vagrant.

### `hieradata` path messiness

There's no way to provide variables to Hiera for path expansion other than via
Puppet itself. It's a bit chicken-and-eggy. The provided `hiera.yaml` uses a
relative path to the `hieradata` directory that is compatible with the Vagrant
invocation of Puppet, and the example `puppet_apply.sh` similarly takes care of
the path munging, but it doesn't feel all that clean to me.

[puppet]: http://puppetlabs.com
[vagrant]: http://vagrantup.com
31 changes: 31 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- mode: ruby -*-

Vagrant::Config.run do |config|
# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "CentOS-6.3-x86_64-reallyminimal"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "https://s3.amazonaws.com/1412126a-vagrant/CentOS-6.3-x86_64-reallyminimal.box"

# Assign this VM to a host-only network IP, allowing you to access it
# via the IP.
config.vm.network :hostonly, "192.168.42.42"

# Enable provisioning with Puppet stand alone.
config.vm.provision :puppet do |puppet|
## tell Puppet where to find the hiera config
puppet.options = "--hiera_config hiera.yaml"

## boilerplate Vagrant/Puppet config
puppet.manifests_path = "puppet/manifests"
puppet.manifest_file = "default.pp"
puppet.module_path = "puppet/modules"

## custom facts provided to Puppet
puppet.facter = {
## tells default.pp that we're running in Vagrant
"is_vagrant" => true,
}
end
end
11 changes: 11 additions & 0 deletions puppet/manifests/default.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## common entry-point into Puppet config.

## if is_vagrant is defined, then we're running under Vagrant. Use other
## logic/facts to detect environmental stuff.
if $::is_vagrant {
$data_center = 'vagrant'
} else {
$data_center = 'amazon'
}

include role::ui
12 changes: 12 additions & 0 deletions puppet/manifests/hiera.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
:hierarchy:
- %{::data_center}
- common

:backends:
- json

:json:
# @todo replace with absolute path for non-Vagrant use
# alternatively set a top-level variable in default.pp and do %{::var_name}
:datadir: 'hieradata'
3 changes: 3 additions & 0 deletions puppet/manifests/hieradata/amazon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"role::ui::public_hostname": "%{::ec2_public_hostname}"
}
2 changes: 2 additions & 0 deletions puppet/manifests/hieradata/common.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
3 changes: 3 additions & 0 deletions puppet/manifests/hieradata/vagrant.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"role::ui::public_hostname": "%{::ipaddress_eth1}"
}
8 changes: 8 additions & 0 deletions puppet/modules/role/manifests/ui.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# public_hostname will be looked up in hiera as role::ui::public_hostname
class role::ui (
$public_hostname
) {
notify {'role::ui::notify':
message => "role::ui configured for ${::data_center}; hostname: $public_hostname",
}
}
11 changes: 11 additions & 0 deletions puppet/puppet_apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

basedir="$( dirname $( readlink -f "${0}" ) )"

## required for the relative datadir path in hiera.yaml
cd ${basedir}/manifests

exec puppet apply \
--hiera_config ${basedir}/manifests/hiera.yaml \
--modulepath ${basedir}/modules \
${basedir}/manifests/default.pp

0 comments on commit 0aceb65

Please sign in to comment.