diff --git a/async_status b/async_status index 15c1fb527f9..38049e97341 100755 --- a/async_status +++ b/async_status @@ -65,7 +65,3 @@ def main(): # this is magic, see lib/ansible/module_common.py #<> main() - - - - diff --git a/failtest b/failtest deleted file mode 100755 index 10b1e63d031..00000000000 --- a/failtest +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/python - -# (c) 2012, Michael DeHaan -# -# 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 . - -import sys - -try: - import json -except ImportError: - import simplejson as json - -print >>sys.stderr, "THIS IS A TEST FAILURE" - -print json.dumps({ - "failed" : True, - "msg" : "this module always fails" -}) - - diff --git a/file b/file index e39e55bef12..1a506689899 100755 --- a/file +++ b/file @@ -17,34 +17,16 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import os -import sys -import shlex -import subprocess import shutil import stat import grp import pwd -import syslog try: import selinux HAVE_SELINUX=True except ImportError: HAVE_SELINUX=False -def dump_kv(vars): - return " ".join("%s='%s'" % (k,v) for (k,v) in vars.items()) - -def exit_kv(rc=0, **kwargs): - if 'path' in kwargs: - add_path_info(kwargs) - print dump_kv(kwargs) - sys.exit(rc) - -def fail_kv(**kwargs): - kwargs['failed'] = True - exit_kv(rc=1, **kwargs) - def add_path_info(kwargs): path = kwargs['path'] if os.path.exists(path): @@ -66,6 +48,14 @@ def add_path_info(kwargs): kwargs['state'] = 'absent' return kwargs +def module_exit_json(**kwargs): + add_path_info(kwargs) + module.exit_json(**kwargs) + +def module_fail_json(**kwargs): + add_path_info(kwargs) + module.fail_json(**kwargs) + # Detect whether using selinux that is MLS-aware. # While this means you can set the level/range with # selinux.lsetfilecon(), it may or may not mean that you @@ -115,7 +105,7 @@ def selinux_context(path): try: ret = selinux.lgetfilecon(path) except: - fail_kv(path=path, msg='failed to retrieve selinux context') + module_fail_json(path=path, msg='failed to retrieve selinux context') if ret[0] == -1: return context context = ret[1].split(':') @@ -123,56 +113,10 @@ def selinux_context(path): # =========================================== -argfile = sys.argv[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) -if not len(items): - fail_kv(msg='the module requires arguments -a') - sys.exit(1) -params = {} -for x in items: - (k, v) = x.split("=",1) - params[k] = v -state = params.get('state','file') -path = params.get('path', params.get('dest', params.get('name', None))) -if path: - path = os.path.expanduser(path) -src = params.get('src', None) -if src: - src = os.path.expanduser(src) -dest = params.get('dest', None) -mode = params.get('mode', None) -owner = params.get('owner', None) -group = params.get('group', None) -# selinux related options -seuser = params.get('seuser', None) -serole = params.get('serole', None) -setype = params.get('setype', None) -selevel = params.get('serange', 's0') -secontext = [seuser, serole, setype] -if selinux_mls_enabled(): - secontext.append(selevel) - -default_secontext = selinux_default_context(path) -for i in range(len(default_secontext)): - if i is not None and secontext[i] == '_default': - secontext[i] = default_secontext[i] - -if state not in [ 'file', 'directory', 'link', 'absent']: - fail_kv(msg='invalid state: %s' % state) - -if state == 'link' and (src is None or dest is None): - fail_kv(msg='src and dest are required for "link" state') -elif path is None: - fail_kv(msg='path is required') - -changed = False # =========================================== # support functions @@ -205,9 +149,9 @@ def set_context_if_different(path, context, changed): try: rc = selinux.lsetfilecon(path, ':'.join(new_context)) except OSError: - fail_kv(path=path, msg='invalid selinux context') + module_fail_json(path=path, msg='invalid selinux context') if rc != 0: - fail_kv(path=path, msg='set selinux context failed') + module_fail_json(path=path, msg='set selinux context failed') changed = True return changed @@ -218,7 +162,7 @@ def set_owner_if_different(path, owner, changed): if owner != user: rc = os.system("/bin/chown -R %s %s 2>/dev/null" % (owner, path)) if rc != 0: - fail_kv(path=path, msg='chown failed') + module_fail_json(path=path, msg='chown failed') return True return changed @@ -230,7 +174,7 @@ def set_group_if_different(path, group, changed): if old_group != group: rc = os.system("/bin/chgrp -R %s %s" % (group, path)) if rc != 0: - fail_kv(path=path, msg='chgrp failed') + module_fail_json(path=path, msg='chgrp failed') return True return changed @@ -241,7 +185,7 @@ def set_mode_if_different(path, mode, changed): # FIXME: support English modes mode = int(mode, 8) except Exception, e: - fail_kv(path=path, msg='mode needs to be something octalish', details=str(e)) + module_fail_json(path=path, msg='mode needs to be something octalish', details=str(e)) st = os.stat(path) prev_mode = stat.S_IMODE(st[stat.ST_MODE]) @@ -252,7 +196,7 @@ def set_mode_if_different(path, mode, changed): try: os.chmod(path, mode) except Exception, e: - fail_kv(path=path, msg='chmod failed', details=str(e)) + module_fail_json(path=path, msg='chmod failed', details=str(e)) st = os.stat(path) new_mode = stat.S_IMODE(st[stat.ST_MODE]) @@ -263,98 +207,150 @@ def set_mode_if_different(path, mode, changed): def rmtree_error(func, path, exc_info): - fail_kv(path=path, msg='failed to remove directory') + module_fail_json(path=path, msg='failed to remove directory') # =========================================== # go... -prev_state = 'absent' -if os.path.lexists(path): - if os.path.islink(path): - prev_state = 'link' - elif os.path.isfile(path): - prev_state = 'file' - else: - prev_state = 'directory' +def main(): -if prev_state != 'absent' and state == 'absent': - try: - if prev_state == 'directory': - if os.path.islink(path): - os.unlink(path) - else: - shutil.rmtree(path, ignore_errors=False, onerror=rmtree_error) + global module + module = AnsibleModule( + argument_spec = dict( + state = dict(choices=['file','directory','link','absent'], default='file'), + path = dict(aliases=['dest', 'name'], required=True), + src = dict(), + mode = dict(), + owner = dict(), + group = dict(), + seuser = dict(), + serole = dict(), + selevel = dict(), + secontext = dict(), + ) + ) + + params = module.params + state = params['state'] + path = os.path.expanduser(params['path']) + src = params.get('src', None) + if src: + src = os.path.expanduser(src) + + mode = params.get('mode', None) + owner = params.get('owner', None) + group = params.get('group', None) + + # selinux related options + seuser = params.get('seuser', None) + serole = params.get('serole', None) + setype = params.get('setype', None) + selevel = params.get('serange', 's0') + secontext = [seuser, serole, setype] + if selinux_mls_enabled(): + secontext.append(selevel) + + default_secontext = selinux_default_context(path) + for i in range(len(default_secontext)): + if i is not None and secontext[i] == '_default': + secontext[i] = default_secontext[i] + + if state == 'link' and (src is None or path is None): + module_fail_json(msg='src and dest are required for "link" state') + elif path is None: + module_fail_json(msg='path is required') + + changed = False + + prev_state = 'absent' + if os.path.lexists(path): + if os.path.islink(path): + prev_state = 'link' + elif os.path.isfile(path): + prev_state = 'file' else: - os.unlink(path) - except Exception, e: - fail_kv(path=path, msg=str(e)) - exit_kv(path=path, changed=True) - sys.exit(0) + prev_state = 'directory' -if prev_state != 'absent' and prev_state != state: - fail_kv(path=path, msg='refusing to convert between %s and %s' % (prev_state, state)) + if prev_state != 'absent' and state == 'absent': + try: + if prev_state == 'directory': + if os.path.islink(path): + os.unlink(path) + else: + shutil.rmtree(path, ignore_errors=False, onerror=rmtree_error) + else: + os.unlink(path) + except Exception, e: + module_fail_json(path=path, msg=str(e)) + module_exit_json(path=path, changed=True) -if prev_state == 'absent' and state == 'absent': - exit_kv(path=path, changed=False) + if prev_state != 'absent' and prev_state != state: + module_fail_json(path=path, msg='refusing to convert between %s and %s' % (prev_state, state)) -if state == 'file': + if prev_state == 'absent' and state == 'absent': + module_exit_json(path=path, changed=False) - if prev_state == 'absent': - fail_kv(path=path, msg='file does not exist, use copy or template module to create') - - # set modes owners and context as needed - changed = set_context_if_different(path, secontext, changed) - changed = set_owner_if_different(path, owner, changed) - changed = set_group_if_different(path, group, changed) - changed = set_mode_if_different(path, mode, changed) - - exit_kv(path=path, changed=changed) - -elif state == 'directory': - - if prev_state == 'absent': - os.makedirs(path) - changed = True + if state == 'file': - # set modes owners and context as needed - changed = set_context_if_different(path, secontext, changed) - changed = set_owner_if_different(path, owner, changed) - changed = set_group_if_different(path, group, changed) - changed = set_mode_if_different(path, mode, changed) + if prev_state == 'absent': + module_fail_json(path=path, msg='file does not exist, use copy or template module to create') - exit_kv(path=path, changed=changed) + # set modes owners and context as needed + changed = set_context_if_different(path, secontext, changed) + changed = set_owner_if_different(path, owner, changed) + changed = set_group_if_different(path, group, changed) + changed = set_mode_if_different(path, mode, changed) -elif state == 'link': + module_exit_json(path=path, changed=changed) + + elif state == 'directory': + + if prev_state == 'absent': + os.makedirs(path) + changed = True + + # set modes owners and context as needed + changed = set_context_if_different(path, secontext, changed) + changed = set_owner_if_different(path, owner, changed) + changed = set_group_if_different(path, group, changed) + changed = set_mode_if_different(path, mode, changed) + + module_exit_json(path=path, changed=changed) + + elif state == 'link': - if os.path.isabs(src): - abs_src = src - else: - abs_src = os.path.join(os.path.dirname(dest), src) - if not os.path.exists(abs_src): - fail_kv(dest=dest, src=src, msg='src file does not exist') + if os.path.isabs(src): + abs_src = src + else: + abs_src = os.path.join(os.path.dirname(dest), src) + if not os.path.exists(abs_src): + module_fail_json(dest=dest, src=src, msg='src file does not exist') - if prev_state == 'absent': - os.symlink(src, dest) - changed = True - elif prev_state == 'link': - old_src = os.readlink(dest) - if not os.path.isabs(old_src): - old_src = os.path.join(os.path.dirname(dest), old_src) - if old_src != src: - os.unlink(dest) - os.symlink(src, dest) - else: - fail_kv(dest=dest, src=src, msg='unexpected position reached') + if prev_state == 'absent': + os.symlink(src, path) + changed = True + elif prev_state == 'link': + old_src = os.readlink(path) + if not os.path.isabs(old_src): + old_src = os.path.join(os.path.dirname(path), old_src) + if old_src != src: + os.unlink(path) + os.symlink(src, path) + else: + module_fail_json(dest=path, src=src, msg='unexpected position reached') - # set modes owners and context as needed - changed = set_context_if_different(dest, secontext, changed) - changed = set_owner_if_different(dest, owner, changed) - changed = set_group_if_different(dest, group, changed) - changed = set_mode_if_different(dest, mode, changed) + # set modes owners and context as needed + changed = set_context_if_different(path, secontext, changed) + changed = set_owner_if_different(path, owner, changed) + changed = set_group_if_different(path, group, changed) + changed = set_mode_if_different(path, mode, changed) - exit_kv(dest=dest, src=src, changed=changed) + module.exit_json(dest=path, src=src, changed=changed) + + module_fail_json(path=path, msg='unexpected position reached') + +# this is magic, see lib/ansible/module_common.py +#<> +main() -fail_kv(path=path, msg='unexpected position reached') -sys.exit(0) - diff --git a/get_url b/get_url index 4d82f8abfcb..bb125d94949 100755 --- a/get_url +++ b/get_url @@ -19,9 +19,7 @@ # # see examples/playbooks/get_url.yml -import os import shutil -import syslog import datetime import tempfile @@ -188,5 +186,4 @@ def main(): # this is magic, see lib/ansible/module_common.py #<> - main() diff --git a/git b/git index d493df7bef0..505209ec3fa 100755 --- a/git +++ b/git @@ -22,9 +22,7 @@ # tag. Latest is not supported, you should not be doing # that. Contribs welcome! -- MPD -import os import re -import subprocess def get_version(dest): ''' samples the version of the git repo ''' diff --git a/group b/group index 489a1ede8cf..93a8ae50891 100755 --- a/group +++ b/group @@ -17,9 +17,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import os import grp -import subprocess def get_bin_path(module, arg): if os.path.exists('/usr/sbin/%s' % arg): diff --git a/mount b/mount index 31a74f158ce..3ce3a9ed1de 100755 --- a/mount +++ b/mount @@ -19,13 +19,16 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -#mount module - mount fs and define in fstab -#usage: -# mount name=mountpoint, src=device_to_be_mounted fstype=fstype opts=mount_opts, dump=0 passno=0 state=[present|absent|mounted|unmounted] -# absent == remove from fstab and unmounted -# present == add to fstab, do not change mount state -# mounted == add to fstab if not there and make sure it is mounted -# unmounted == do not change fstab state, but unmount +# mount module - mount fs and define in fstab +# usage: +# +# mount name=mountpoint, src=device_to_be_mounted fstype=fstype +# opts=mount_opts, dump=0 passno=0 state=[present|absent|mounted|unmounted] +# +# absent == remove from fstab and unmounted +# present == add to fstab, do not change mount state +# mounted == add to fstab if not there and make sure it is mounted +# unmounted == do not change fstab state, but unmount def write_fstab(lines, dest): @@ -37,14 +40,15 @@ def write_fstab(lines, dest): fs_w.close() def set_mount(**kwargs): - "set/change a mount point location in fstab" + """ set/change a mount point location in fstab """ + # kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab - args = { - 'opts': 'defaults', - 'dump': '0', - 'passno': '0', - 'fstab': '/etc/fstab' - } + args = dict( + opts = 'defaults', + dump = '0', + passno = '0', + fstab = '/etc/fstab' + ) args.update(kwargs) new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n' @@ -84,7 +88,6 @@ def set_mount(**kwargs): else: to_write.append(line) - if not exists: to_write.append(new_line % args) changed = True @@ -96,14 +99,15 @@ def set_mount(**kwargs): def unset_mount(**kwargs): - "remove a mount point from fstab" + """ remove a mount point from fstab """ + # kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab - args = { - 'opts': 'default', - 'dump': '0', - 'passno': '0', - 'fstab': '/etc/fstab' - } + args = dict( + opts = 'default', + dump = '0', + passno = '0', + fstab = '/etc/fstab' + ) args.update(kwargs) to_write = [] @@ -138,7 +142,8 @@ def unset_mount(**kwargs): def mount(**kwargs): - "mount up a path or remount if needed" + """ mount up a path or remount if needed """ + name = kwargs['name'] if os.path.ismount(name): cmd = ['/bin/mount', '-o', 'remount', name] @@ -153,7 +158,8 @@ def mount(**kwargs): return call.returncode, out+err def umount(**kwargs): - "unmount a path" + """ unmount a path """ + name = kwargs['name'] cmd = ['/bin/umount', name] @@ -165,6 +171,7 @@ def umount(**kwargs): return call.returncode, out+err def main(): + module = AnsibleModule( argument_spec = dict( state = dict(required=True, choices=['present', 'absent', 'mounted', 'unmounted']), @@ -194,7 +201,6 @@ def main(): if module.params['fstab'] is not None: args['fstab'] = module.params['fstab'] - # absent == remove from fstab and unmounted # unmounted == do not change fstab state, but unmount # present == add to fstab, do not change mount state @@ -218,7 +224,6 @@ def main(): module.exit_json(changed=changed, **args) - if state == 'unmounted': if os.path.ismount(name): res,msg = umount(**args) @@ -228,8 +233,6 @@ def main(): module.exit_json(changed=changed, **args) - - if state in ['mounted', 'present']: name, changed = set_mount(**args) if state == 'mounted': @@ -258,5 +261,4 @@ def main(): # this is magic, see lib/ansible/module_common.py #<> - main() diff --git a/ohai b/ohai index b0e8fb4bbfc..2a1e36ea763 100755 --- a/ohai +++ b/ohai @@ -18,21 +18,16 @@ # along with Ansible. If not, see . # -import subprocess - def get_facter_data(): - p = subprocess.Popen(["/usr/bin/env", "ohai"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = p.communicate() rc = p.returncode return rc, out, err def main(): - module = AnsibleModule( argument_spec = dict() ) - rc, out, err = get_facter_data() if rc != 0: module.fail_json(msg=err) diff --git a/ping b/ping index 34d097864d9..1be774fcc74 100755 --- a/ping +++ b/ping @@ -17,8 +17,6 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import base64 - def main(): module = AnsibleModule( argument_spec = dict() diff --git a/postgresql_db b/postgresql_db index 478b376dc7d..890096dee2d 100755 --- a/postgresql_db +++ b/postgresql_db @@ -26,19 +26,16 @@ else: # PostgreSQL module specific support methods. # - def db_exists(cursor, db): query = "SELECT * FROM pg_database WHERE datname=%(db)s" cursor.execute(query, {'db': db}) return cursor.rowcount == 1 - def db_delete(cursor, db): query = "DROP DATABASE %s" % db cursor.execute(query) return True - def db_create(cursor, db): query = "CREATE DATABASE %s" % db cursor.execute(query) @@ -48,7 +45,6 @@ def db_create(cursor, db): # Module execution. # - def main(): module = AnsibleModule( argument_spec=dict( diff --git a/setup b/setup index 455fb5b4bf9..5a9ab9bef0c 100755 --- a/setup +++ b/setup @@ -20,14 +20,10 @@ import array import fcntl import glob -import sys -import os import platform import re import socket import struct -import subprocess -import traceback try: import selinux diff --git a/slurp b/slurp index bc9f643d016..2bede8a9701 100755 --- a/slurp +++ b/slurp @@ -17,8 +17,6 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import base64 - def main(): module = AnsibleModule( argument_spec = dict( diff --git a/user b/user index ce0a88729c9..5b653c537c5 100755 --- a/user +++ b/user @@ -17,10 +17,8 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . -import os import pwd import grp -import subprocess try: import spwd HAVE_SPWD=True diff --git a/virt b/virt index a67f8c1bd3e..5de5567ebaa 100755 --- a/virt +++ b/virt @@ -17,11 +17,6 @@ VIRT_FAILED = 1 VIRT_SUCCESS = 0 VIRT_UNAVAILABLE=2 -# other modules -import os -import sys -import subprocess - try: import libvirt except ImportError: diff --git a/yum b/yum index ac5adaf892b..f62cb8a2f06 100755 --- a/yum +++ b/yum @@ -18,7 +18,6 @@ # along with Ansible. If not, see . # - import yum import datetime import traceback