1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Include facter variables for free in setup JSON (prefix with 'facter'.

Also sort keys in JSON file and pretty print
This commit is contained in:
Michael DeHaan 2012-02-27 23:06:32 -05:00
parent e766bb6a6c
commit 186dab4dff

View file

@ -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()