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.
reviewable/pr18780/r1
Michael DeHaan 13 years ago
parent cb6acfc1dc
commit 73d9fbfc9e

60
slurp

@ -17,60 +17,24 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import shlex
import base64 import base64
import syslog
try: # this is magic, see lib/ansible/module_common.py
import json #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
except ImportError:
import simplejson as json
# =========================================== module = AnsibleModule(
# convert arguments of form a=b c=d argument_spec = dict(
# to a dictionary 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): if not os.path.exists(source):
print json.dumps(dict( module.fail_json(msg="file not found: %s" % source)
failed = 1,
msg = "file not found: %s" % source
))
sys.exit(1)
if not os.access(source, os.R_OK): if not os.access(source, os.R_OK):
print json.dumps(dict( module.fail_json(msg="file is not readable: %s" % source)
failed = 1,
msg = "file is not readable: %s" % source
))
sys.exit(1)
# ==========================================
data = file(source).read() data = base64.b64encode(file(source).read())
data = base64.b64encode(data)
print json.dumps(dict(content=data, encoding='base64')) module.exit_json(content=data, encoding='base64')
sys.exit(0)

Loading…
Cancel
Save