Skip to content

Instantly share code, notes, and snippets.

@mgwilliams
Created February 22, 2012 22:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgwilliams/1887753 to your computer and use it in GitHub Desktop.
Save mgwilliams/1887753 to your computer and use it in GitHub Desktop.
Python script to access data from hiera...
from subprocess import check_output
import json
import collections
def hiera(key, environment, location, hostname, merge=None, preserve_order=True, bin="/usr/local/bin/hiera", confdir="/etc/puppet/"):
if preserve_order:
obj_pair_hook=collections.OrderedDict
else:
obj_pair_hook=None
if merge==list:
merge_opt='-a'
elif merge==dict:
merge_opt='-h'
else:
merge_opt=None
args = [bin, key, 'environment=%s' % environment, 'location=%s' % location, 'hostname=%s' % hostname, 'settings::confdir=%s' % confdir]
if merge_opt:
args.append(merge_opt)
o = check_output(args).rstrip()
if o == 'nil':
return None
else:
try:
i = o.replace('=>', ':')
return json.loads(i, object_pairs_hook=obj_pair_hook)
except:
return o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment