Exemplo n.º 1
0
def main():

    module = AnsibleModule(argument_spec=dict(
        state=dict(required=True,
                   choices=['present', 'absent', 'mounted', 'unmounted']),
        name=dict(required=True),
        opts=dict(default=None),
        passno=dict(default=None),
        dump=dict(default=None),
        src=dict(required=True),
        fstype=dict(required=True),
        fstab=dict(default='/etc/fstab')),
                           supports_check_mode=True)

    changed = False
    rc = 0
    args = {
        'name': module.params['name'],
        'src': module.params['src'],
        'fstype': module.params['fstype']
    }
    if module.params['passno'] is not None:
        args['passno'] = module.params['passno']
    if module.params['opts'] is not None:
        args['opts'] = module.params['opts']
    if module.params['dump'] is not None:
        args['dump'] = module.params['dump']
    if module.params['fstab'] is not None:
        args['fstab'] = module.params['fstab']

    # if fstab file does not exist, we first need to create it. This mainly
    # happens when fstab optin is passed to the module.
    if not os.path.exists(args['fstab']):
        if not os.path.exists(os.path.dirname(args['fstab'])):
            os.makedirs(os.path.dirname(args['fstab']))
        open(args['fstab'], 'a').close()

    # absent == remove from fstab and unmounted
    # unmounted == do not change fstab state, but unmount
    # present == add to fstab, do not change mount state
    # mounted == add to fstab if not there and make sure it is mounted, if it has changed in fstab then remount it

    state = module.params['state']
    name = module.params['name']
    if state == 'absent':
        name, changed = unset_mount(module, **args)
        if changed and not module.check_mode:
            if ismount(name):
                res, msg = umount(module, **args)
                if res:
                    module.fail_json(msg="Error unmounting %s: %s" %
                                     (name, msg))

            if os.path.exists(name):
                try:
                    os.rmdir(name)
                except (OSError, IOError), e:
                    module.fail_json(msg="Error rmdir %s: %s" % (name, str(e)))

        module.exit_json(changed=changed, **args)
Exemplo n.º 2
0
def mount(module, **kwargs):
    """ mount up a path or remount if needed """

    # kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
    args = dict(
        opts   = 'default',
        dump   = '0',
        passno = '0',
        fstab  = '/etc/fstab'
    )
    args.update(kwargs)

    mount_bin = module.get_bin_path('mount')

    name = kwargs['name']

    cmd = [ mount_bin, ]

    if ismount(name):
        cmd += [ '-o', 'remount', ]

    if get_platform().lower() == 'freebsd':
        cmd += [ '-F', args['fstab'], ]

    if get_platform().lower() == 'linux':
        cmd += [ '-T', args['fstab'], ]

    cmd += [ name, ]

    rc, out, err = module.run_command(cmd)
    if rc == 0:
        return 0, ''
    else:
        return rc, out+err
Exemplo n.º 3
0
            if ismount(name):
                res, msg = umount(module, **args)
                if res:
                    module.fail_json(msg="Error unmounting %s: %s" %
                                     (name, msg))

            if os.path.exists(name):
                try:
                    os.rmdir(name)
                except (OSError, IOError), e:
                    module.fail_json(msg="Error rmdir %s: %s" % (name, str(e)))

        module.exit_json(changed=changed, **args)

    if state == 'unmounted':
        if ismount(name):
            if not module.check_mode:
                res, msg = umount(module, **args)
                if res:
                    module.fail_json(msg="Error unmounting %s: %s" %
                                     (name, msg))
            changed = True

        module.exit_json(changed=changed, **args)

    if state in ['mounted', 'present']:
        if state == 'mounted':
            if not os.path.exists(name) and not module.check_mode:
                try:
                    os.makedirs(name)
                except (OSError, IOError), e:
Exemplo n.º 4
0
def main():

    module = AnsibleModule(argument_spec=dict(
        state=dict(required=True,
                   choices=['present', 'absent', 'mounted', 'unmounted']),
        name=dict(required=True),
        opts=dict(default=None),
        passno=dict(default=None, type='str'),
        dump=dict(default=None),
        src=dict(required=False),
        fstype=dict(required=False),
        fstab=dict(default='/etc/fstab')),
                           supports_check_mode=True,
                           required_if=([
                               'state', 'mounted', ['src', 'fstype']
                           ], ['state', 'present', ['src', 'fstype']]))

    changed = False
    rc = 0
    args = {'name': module.params['name']}
    if module.params['src'] is not None:
        args['src'] = module.params['src']
    if module.params['fstype'] is not None:
        args['fstype'] = module.params['fstype']
    if module.params['passno'] is not None:
        args['passno'] = module.params['passno']
    if module.params['opts'] is not None:
        args['opts'] = module.params['opts']
    if module.params['dump'] is not None:
        args['dump'] = module.params['dump']
    if module.params['fstab'] is not None:
        args['fstab'] = module.params['fstab']

    # if fstab file does not exist, we first need to create it. This mainly
    # happens when fstab optin is passed to the module.
    if not os.path.exists(args['fstab']):
        if not os.path.exists(os.path.dirname(args['fstab'])):
            os.makedirs(os.path.dirname(args['fstab']))
        open(args['fstab'], 'a').close()

    # absent == remove from fstab and unmounted
    # unmounted == do not change fstab state, but unmount
    # present == add to fstab, do not change mount state
    # mounted == add to fstab if not there and make sure it is mounted, if it has changed in fstab then remount it

    state = module.params['state']
    name = module.params['name']
    if state == 'absent':
        name, changed = unset_mount(module, **args)
        if changed and not module.check_mode:
            if ismount(name):
                res, msg = umount(module, **args)
                if res:
                    module.fail_json(msg="Error unmounting %s: %s" %
                                     (name, msg))

            if os.path.exists(name):
                try:
                    os.rmdir(name)
                except (OSError, IOError):
                    e = get_exception()
                    module.fail_json(msg="Error rmdir %s: %s" % (name, str(e)))

        module.exit_json(changed=changed, **args)

    if state == 'unmounted':
        if ismount(name):
            if not module.check_mode:
                res, msg = umount(module, **args)
                if res:
                    module.fail_json(msg="Error unmounting %s: %s" %
                                     (name, msg))
            changed = True

        module.exit_json(changed=changed, **args)

    if state in ['mounted', 'present']:
        if state == 'mounted':
            if not os.path.exists(name) and not module.check_mode:
                try:
                    os.makedirs(name)
                except (OSError, IOError):
                    e = get_exception()
                    module.fail_json(msg="Error making dir %s: %s" %
                                     (name, str(e)))

        name, changed = set_mount(module, **args)
        if state == 'mounted':
            res = 0
            if ismount(name):
                if changed and not module.check_mode:
                    res, msg = mount(module, **args)
            elif 'bind' in args.get('opts', []):
                changed = True
                cmd = 'mount -l'
                rc, out, err = module.run_command(cmd)
                allmounts = out.split('\n')
                for mounts in allmounts[:-1]:
                    arguments = mounts.split()
                    if arguments[0] == args['src'] and arguments[2] == args[
                            'name'] and arguments[4] == args['fstype']:
                        changed = False
                if changed:
                    res, msg = mount(module, **args)
            else:
                changed = True
                if not module.check_mode:
                    res, msg = mount(module, **args)

            if res:
                module.fail_json(msg="Error mounting %s: %s" % (name, msg))

        module.exit_json(changed=changed, **args)

    module.fail_json(msg='Unexpected position reached')