Added code to allow insertion of boilerplate into modules to make them able to share lots of code, the result

should be a huge reduction of total ansible source, at a slight cost of difficulty in original module development.

We need to apply this now to all modules, but may need to have some exemptions to things like command, which should
subclass this module.
This commit is contained in:
Michael DeHaan 2012-07-17 22:33:36 -04:00
parent cb6acfc1dc
commit 73d9fbfc9e

60
slurp
View file

@ -17,60 +17,24 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import shlex
import base64
import syslog
try:
import json
except ImportError:
import simplejson as json
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
# ===========================================
# convert arguments of form a=b c=d
# to a dictionary
module = AnsibleModule(
argument_spec = dict(
src=(None,True),
)
)
source = module.params['src']
if len(sys.argv) == 1:
sys.exit(1)
argfile = sys.argv[1]
if not os.path.exists(argfile):
sys.exit(1)
args = open(argfile, 'r').read()
items = shlex.split(args)
syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args)
params = {}
for x in items:
(k, v) = x.split("=")
params[k] = v
source = os.path.expanduser(params['src'])
# ==========================================
# raise an error if there is no template metadata
if not os.path.exists(source):
print json.dumps(dict(
failed = 1,
msg = "file not found: %s" % source
))
sys.exit(1)
module.fail_json(msg="file not found: %s" % source)
if not os.access(source, os.R_OK):
print json.dumps(dict(
failed = 1,
msg = "file is not readable: %s" % source
))
sys.exit(1)
module.fail_json(msg="file is not readable: %s" % source)
# ==========================================
data = base64.b64encode(file(source).read())
data = file(source).read()
data = base64.b64encode(data)
print json.dumps(dict(content=data, encoding='base64'))
sys.exit(0)
module.exit_json(content=data, encoding='base64')