template 3.24 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 21 22
import sys
import os
import jinja2
23
import shlex
24
try:
25
    import json
26
except ImportError:
27
    import simplejson as json
28

29 30
environment = jinja2.Environment()

31
# ===========================================
32
# convert arguments of form a=b c=d
33 34 35
# to a dictionary
# FIXME: make more idiomatic

Michael DeHaan committed
36 37 38 39 40 41 42
if len(sys.argv) == 1:
    sys.exit(1)
argfile = sys.argv[1]
if not os.path.exists(argfile):
    sys.exit(1)
items = shlex.split(open(argfile, 'r').read())

43 44 45 46 47 48 49 50
params = {}
for x in items:
    (k, v) = x.split("=")
    params[k] = v

source  = params['src']
dest    = params['dest']
metadata = params.get('metadata', '/etc/ansible/setup') 
51
module_vars = params.get('vars')
52 53 54 55 56 57 58 59 60 61
 
# raise an error if there is no template metadata
if not os.path.exists(metadata):
    print json.dumps({
        "failed" : 1,
        "msg"    : "Missing %s, did you run the setup module yet?" % metadata
    })
    sys.exit(1)

# raise an error if we can't parse the template metadata
62
#data = {}
63 64 65 66 67 68 69 70 71 72 73
try:
   f = open(metadata)
   data = json.loads(f.read())
   f.close()
except:
   print json.dumps({
       "failed" : 1,
       "msg"    : "Failed to parse/load %s, rerun the setup module?" % metadata
   })
   sys.exit(1)

74 75 76 77 78 79 80 81 82 83 84 85 86
if module_vars:
    try:
        f = open(module_vars)
        vars = json.loads(f.read())
        data.update(vars)
        f.close()
    except:
        print json.dumps({
            "failed" : 1,
            "msg"    : "Failed to parse/load %s." % module_vars
        })
        sys.exit(1)

87 88 89 90 91 92 93 94 95
if not os.path.exists(source):
    print json.dumps({
        "failed" : 1,
        "msg"    : "Source template could not be read: %s" % source
    })
    sys.exit(1)

source = file(source).read()

96 97 98 99 100 101 102
if os.path.isdir(dest):
    print json.dumps({
         "failed" : 1,
         "msg"    : "Destination is a directory"
    })
    sys.exit(1)

103 104 105 106
# record md5sum of original source file so we can report if it changed
changed = False
md5sum = None
if os.path.exists(dest):
107
    md5sum = os.popen("md5sum %s" % dest).read().split()[0]
108

109 110 111 112
try:
    # call Jinja2 here and save the new template file
    template = environment.from_string(source)
    data_out = template.render(data)
113
except jinja2.TemplateError, e:
114 115 116 117 118
    print json.dumps({
        "failed": True,
        "msg"   : e.message
    })
    sys.exit(1)
119 120 121 122 123
f = open(dest, "w+")
f.write(data_out)
f.close()

# record m5sum and return success and whether things have changed
124
md5sum2 = os.popen("md5sum %s" % dest).read().split()[0]
125 126

if md5sum != md5sum2:
127
    changed = True
128 129 130 131 132 133 134 135

# mission accomplished
print json.dumps({
   "md5sum"   : md5sum2,
   "changed"  : changed 
})