Puppet data from CouchDB using hiera-http

Puppet data from CouchDB using hiera-http

Introducing hiera-http
I started looking at various places people store data, and ways to fetch it and realized that a lot of data storage applications are RESTful, yet there doesn’t seem to be any support in Hiera to query these things, so I whipped up hiera-http, a Hiera back end to connect to any HTTP RESTful API and return data based on a lookup. It’s very new and support for other stuff like SSL and Auth is coming, but what it does support is a variety of handlers to parse the returned output of the HTTP doc, at the moment these are limited to YAML and JSON (or just ‘plain’ to simply return the whole response of the request). The following is a quick demonstration of how to plug CouchDB into Puppet using Hiera and the hiera-http backend.

Hiera-http is available as a rubygem, or from GitHub: http://github.com/crayfishx/hiera-http

Apache CouchDB is a scalable database that uses no set schema and is ideal for storing configuration data as everything is stored and retrieved as JSON documents. For the purposes of this demo I’m just going to do a very simple database installation with three documents and a few configuration parameters to demonstrate how to integrate this in with Puppet.

After installing couchdb and starting the service I’m able to access Futon, the web GUI front-end for my couchdb service – using this I create three documents, “dev”, “common” and “puppet.puppetlabs.lan”

CouchDB documents
CouchDB documents
Next I populate my common and dev documents with some variables.

Common document populated with data
Now CouchDB is configured I should be able to query the data over HTTP

[root@puppet ~]# telnet 5984
Connected to (
Escape character is ‘^]’.
GET /configuration/common HTTP/1.0

HTTP/1.0 200 OK
Server: CouchDB/1.0.2 (Erlang OTP/R12B)
Etag: “2-2ffb42b336d8d920ef46ac1865c491aa”
Date: Mon, 29 Oct 2012 18:45:48 GMT
Content-Type: text/plain;charset=utf-8
Content-Length: 137
Cache-Control: must-revalidate

Connection closed by foreign host.
[root@puppet ~]#
Query with Hiera
After installing hiera-http I can query this data directly from Hiera…

# gem install hiera-http
First I need to configure Hiera with the HTTP back end. The search hierarchy is determined by the :paths: configuration parameter and since CouchDB returns JSON I set that as the output handler.

:backends: [‘http’]

:port: 5984
:output: json
:failure: graceful
– /configuration/%{fqdn}
– /configuration/%{environment}
– /configuration/common
I can now query this directly from Hiera on the command line

[root@puppet /]# hiera dnsservers
[“”, “”]

[root@puppet /]# hiera ntpserver
And of course, that means that this data is now available from Puppet and if I add some overriding configuration variables to my dev document in CouchDB, my lookup will resolve based on my environment setting in Puppet

notify { $ntpserver: }
[root@puppet /]# puppet apply test.pp
notice: /Stage[main]//Notify[]/message: defined ‘message’ as ‘’
notice: Finished catalog run in 0.04 seconds

[root@puppet /]# puppet apply test.pp –environment=dev
notice: /Stage[main]//Notify[]/message: defined ‘message’ as ‘’
notice: Finished catalog run in 0.04 seconds
Hiera-http is fully featured and supports all standard Hiera back end functions such as hiera_hash, hiera_array order overrides.

Future stuff
I’m going to carry on working on new features for hiera-http – including basic auth, HTTPS/SSL, proxys and a wider variety of output handlers – I would like for this back end to be flexible enough to allow users to configure Hiera with any network service that uses a RESTful API to perform data lookups. Keep watching.

Subscribe to Craig Dunn

Sign up now to get access to the library of members-only issues.
Jamie Larson