1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00
community.general/library/setup
Michael DeHaan 186dab4dff Include facter variables for free in setup JSON (prefix with 'facter'.
Also sort keys in JSON file and pretty print
2012-02-27 23:06:32 -05:00

68 lines
1.5 KiB
Python
Executable file

#!/usr/bin/python
DEFAULT_ANSIBLE_SETUP = "/etc/ansible/setup"
import sys
import os
import shlex
import subprocess
try:
import json
except ImportError:
import simplejson as json
# load config & template variables
input_data = sys.argv[1:]
new_options = dict([ x.split('=') for x in input_data ])
ansible_file = new_options.get('metadata', DEFAULT_ANSIBLE_SETUP)
ansible_dir = os.path.dirname(ansible_file)
# create the config dir if it doesn't exist
if not os.path.exists(ansible_dir):
os.makedirs(ansible_dir)
changed = False
md5sum = None
if not os.path.exists(ansible_file):
changed = True
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, sort_keys=True, indent=4)
f.write(reformat)
f.close()
md5sum2 = os.popen("md5sum %s" % ansible_file).read()
if md5sum != md5sum2:
changed = True
result = {
"changed" : changed,
"md5sum" : md5sum
}
print json.dumps(result)