Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
Improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
ripienaar committed Jun 6, 2011
1 parent 583f24c commit 59ae8a3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -117,6 +117,15 @@ To achieve this setup the module user needs to configure Hiera in _/etc/puppet/h
:datasource: data
</pre>

Converting from extlookup?
==========================

A simple converter is included called _extlookup2hiera_ and it can convert from CSV to JSON or YAML:

<pre>
$ extlookup2hiera --in common.csv --out common.json --json
</pre>

Installation?
=============

Expand Down
7 changes: 4 additions & 3 deletions Rakefile
Expand Up @@ -4,18 +4,19 @@ require 'rspec/core/rake_task'

spec = Gem::Specification.new do |s|
s.name = "hiera-puppet"
s.version = "0.0.1"
s.version = "0.1.0"
s.author = "R.I.Pienaar"
s.email = "rip@devco.net"
s.homepage = "http://devco.net/"
s.homepage = "https://github.com/ripienaar/hiera-puppet"
s.platform = Gem::Platform::RUBY
s.summary = "Puppet query interface and backend for Hiera"
s.description = "Store and query Hiera data from Puppet"
s.files = FileList["{bin,lib}/**/*"].to_a
s.require_path = "lib"
s.test_files = FileList["{spec}/**/*test.rb"].to_a
s.test_files = FileList["spec/**/*test.rb"].to_a
s.has_rdoc = true
s.add_dependency 'hiera'
s.executables = "extlookup2hiera"
end

Rake::GemPackageTask.new(spec) do |pkg|
Expand Down
56 changes: 56 additions & 0 deletions bin/extlookup2hiera
@@ -0,0 +1,56 @@
#!/usr/bin/env ruby

require 'optparse'
require 'csv'

options = {:in => nil, :out => nil, :format => :yaml}

OptionParser.new do |opts|
opts.banner = "Converter for extlookup CSV files into Hiera JSON and YAML files"

opts.on("--in FILE", "-i", "Input CSV file") do |v|
options[:in] = v
end

opts.on("--out FILE", "-o", "Output Hiera file") do |v|
options[:out] = v
end

opts.on("--json", "-j", "Create JSON format file") do |v|
options[:format] = :json
end
end.parse!

if options[:in].nil? || options[:out].nil?
STDERR.puts "Please specify an input and output file with --in and --out"
exit 1
end

unless File.exist?(options[:in])
STDERR.puts "Cannot find input file #{options[:in]}"
exit 1
end

csvdata = CSV.read(options[:in])
hieradata = {}

csvdata.each do |d|
d = d.map{|item| item.to_s}

if d.size > 2
hieradata[d[0]] = d[1, d.size].flatten
else
hieradata[d[0]] = d[1]
end
end

case options[:format]
when :yaml
require 'yaml'
File.open(options[:out], "w") {|f| f.write hieradata.to_yaml}

when :json
require 'rubygems'
require 'json'
File.open(options[:out], "w") {|f| f.write JSON.pretty_generate hieradata}
end
13 changes: 9 additions & 4 deletions example/README.md
Expand Up @@ -55,19 +55,24 @@ server ntp1.dc1.example.com
server ntp2.dc1.example.com
</pre>

Now simulate a machine in _dc2_:
Now simulate a machine in _dc2_, because there is no data for dc2 it uses the site wide defaults

<pre>
$ FACTER_location=dc2 puppet --config etc/puppet.conf --libdir ../lib site.pp
warning: Could not find class data::dc2 for nephilim.ml.org
notice: /Stage[main]/Ntp::Config/File[/tmp/ntp.conf]/content: content changed '{md5}074d0e2ac727f6cb9afe3345d574b578' to '{md5}8f9039fe1989a278a0a8e1836acb8d23'
$ cat /tmp/ntp.conf
server ntp1.example.com
server ntp2.example.com
</pre>

You could create override data in the following places for a machine in _location=dc1_, they will be searched in this order and the first one with data will match:
You could create override data in the following places for a machine in _location=dc2_, they will be searched in this order and the first one with data will match.

* file etc/hieradb/dc1.yaml
* file etc/hieradb/dc2.yaml
* file etc/hieradb/common.yaml
* class data::dc1
* class data::dc2
* class data::common
* class ntp::config::data
* class ntp::data

In this example due to the presence of _common.yaml_ that declares _ntpservers_ the classes will never be searched, it will have precedence.

0 comments on commit 59ae8a3

Please sign in to comment.