setup 2.87 KB
Newer Older
1 2
#!/usr/bin/python

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# (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/>.

20
DEFAULT_ANSIBLE_SETUP     = "/etc/ansible/setup"
21 22 23 24

import sys
import os
import shlex
25
import subprocess
26 27 28 29 30 31 32 33 34

try:
    import json
except ImportError:
    import simplejson as json

# load config & template variables

input_data = sys.argv[1:]
35 36
new_options = dict([ x.split('=') for x in input_data ])
ansible_file = new_options.get('metadata', DEFAULT_ANSIBLE_SETUP)
37
ansible_dir = os.path.dirname(ansible_file)
38 39 40

# create the config dir if it doesn't exist

41 42
if not os.path.exists(ansible_dir):
    os.makedirs(ansible_dir)
43 44

changed = False
45
md5sum = None
46
if not os.path.exists(ansible_file):
47 48
    changed = True
else:
Michael DeHaan committed
49
    md5sum = os.popen("md5sum %s" % ansible_file).read().split()[0]
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# 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

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
# 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

86 87 88
# write the template/settings file using
# instructions from server

89
f = open(ansible_file, "w+")
90
reformat = json.dumps(new_options, sort_keys=True, indent=4)
91 92 93
f.write(reformat)
f.close()

Michael DeHaan committed
94
md5sum2 = os.popen("md5sum %s" % ansible_file).read().split()[0]
95 96 97 98

if md5sum != md5sum2:
   changed = True

99 100 101 102 103
new_options['written'] = ansible_file
new_options['changed'] = changed
new_options['md5sum']  = md5sum2 

print json.dumps(new_options)
104