From 0ac8aab83ea80c140205adcf67c485867d026dcb Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 27 Feb 2012 23:06:32 -0500 Subject: [PATCH] Include facter variables for free in setup JSON (prefix with 'facter'. Also sort keys in JSON file and pretty print --- setup | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/setup b/setup index 4d93fba9e12..be9e3ab4394 100755 --- a/setup +++ b/setup @@ -5,6 +5,7 @@ DEFAULT_ANSIBLE_SETUP = "/etc/ansible/setup" import sys import os import shlex +import subprocess try: import json @@ -30,11 +31,27 @@ if not os.path.exists(ansible_file): else: md5sum = os.popen("md5sum %s" % ansible_file).read() +# if facter is installed, and we can use --json because +# ruby-json is ALSO installed, include facter data in the JSON + +if os.path.exists("/usr/bin/facter"): + cmd = subprocess.Popen("/usr/bin/facter --json", shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = cmd.communicate() + facter = True + try: + facter_ds = json.loads(out) + except: + facter = False + if facter: + for (k,v) in facter_ds.items(): + new_options["facter_%s" % k] = v + # write the template/settings file using # instructions from server f = open(ansible_file, "w+") -reformat = json.dumps(new_options) +reformat = json.dumps(new_options, sort_keys=True, indent=4) f.write(reformat) f.close()