2012-02-24 08:25:09 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2012-02-29 00:08:09 +00:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-02-27 00:21:44 +00:00
|
|
|
DEFAULT_ANSIBLE_SETUP = "/etc/ansible/setup"
|
2012-02-24 08:25:09 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shlex
|
2012-02-28 04:06:32 +00:00
|
|
|
import subprocess
|
2012-02-24 08:25:09 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
import simplejson as json
|
|
|
|
|
|
|
|
# load config & template variables
|
|
|
|
|
2012-03-14 23:05:19 +00:00
|
|
|
if len(sys.argv) == 1:
|
|
|
|
sys.exit(1)
|
2012-03-22 00:00:48 +00:00
|
|
|
|
2012-03-14 23:05:19 +00:00
|
|
|
argfile = sys.argv[1]
|
|
|
|
if not os.path.exists(argfile):
|
|
|
|
sys.exit(1)
|
2012-03-22 00:00:48 +00:00
|
|
|
|
2012-03-14 23:06:35 +00:00
|
|
|
input_data = shlex.split(open(argfile, 'r').read())
|
2012-03-14 23:05:19 +00:00
|
|
|
|
2012-03-22 00:00:48 +00:00
|
|
|
# turn urlencoded k=v string (space delimited) to regular k=v directionary
|
|
|
|
splitted = [x.split('=',1) for x in input_data ]
|
|
|
|
splitted = [ (x[0], x[1].replace("~~~"," ")) for x in splitted ]
|
|
|
|
new_options = dict(splitted)
|
|
|
|
|
2012-02-27 00:21:44 +00:00
|
|
|
ansible_file = new_options.get('metadata', DEFAULT_ANSIBLE_SETUP)
|
2012-02-27 00:55:26 +00:00
|
|
|
ansible_dir = os.path.dirname(ansible_file)
|
2012-02-24 08:25:09 +00:00
|
|
|
|
|
|
|
# create the config dir if it doesn't exist
|
|
|
|
|
2012-02-27 00:21:44 +00:00
|
|
|
if not os.path.exists(ansible_dir):
|
|
|
|
os.makedirs(ansible_dir)
|
2012-02-24 08:25:09 +00:00
|
|
|
|
|
|
|
changed = False
|
2012-02-27 00:55:26 +00:00
|
|
|
md5sum = None
|
2012-02-27 00:21:44 +00:00
|
|
|
if not os.path.exists(ansible_file):
|
2012-02-24 08:25:09 +00:00
|
|
|
changed = True
|
|
|
|
else:
|
2012-04-04 15:55:24 +00:00
|
|
|
md5sum = os.popen("md5sum %s" % ansible_file).read().split()[0]
|
2012-02-24 08:25:09 +00:00
|
|
|
|
2012-02-28 04:06:32 +00:00
|
|
|
# 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
|
|
|
|
|
2012-02-28 04:15:11 +00:00
|
|
|
# ditto for ohai, but just top level string keys
|
|
|
|
# because it contains a lot of nested stuff we can't use for
|
|
|
|
# templating w/o making a nicer key for it (TODO)
|
|
|
|
|
|
|
|
if os.path.exists("/usr/bin/ohai"):
|
|
|
|
cmd = subprocess.Popen("/usr/bin/ohai", shell=True,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
out, err = cmd.communicate()
|
|
|
|
ohai = True
|
|
|
|
try:
|
|
|
|
ohai_ds = json.loads(out)
|
|
|
|
except:
|
|
|
|
ohai = False
|
|
|
|
if ohai:
|
|
|
|
for (k,v) in ohai_ds.items():
|
|
|
|
if type(v) == str or type(v) == unicode:
|
|
|
|
k2 = "ohai_%s" % k
|
|
|
|
new_options[k2] = v
|
|
|
|
|
2012-02-24 08:25:09 +00:00
|
|
|
# write the template/settings file using
|
|
|
|
# instructions from server
|
|
|
|
|
2012-02-27 00:21:44 +00:00
|
|
|
f = open(ansible_file, "w+")
|
2012-02-28 04:06:32 +00:00
|
|
|
reformat = json.dumps(new_options, sort_keys=True, indent=4)
|
2012-02-24 08:25:09 +00:00
|
|
|
f.write(reformat)
|
|
|
|
f.close()
|
|
|
|
|
2012-04-04 15:55:24 +00:00
|
|
|
md5sum2 = os.popen("md5sum %s" % ansible_file).read().split()[0]
|
2012-02-24 08:25:09 +00:00
|
|
|
|
|
|
|
if md5sum != md5sum2:
|
|
|
|
changed = True
|
|
|
|
|
2012-03-06 01:09:03 +00:00
|
|
|
new_options['written'] = ansible_file
|
|
|
|
new_options['changed'] = changed
|
|
|
|
new_options['md5sum'] = md5sum2
|
|
|
|
|
|
|
|
print json.dumps(new_options)
|
2012-02-24 08:25:09 +00:00
|
|
|
|