2012-07-15 18:38:40 +00:00
|
|
|
#!/usr/bin/python
|
2012-08-03 01:29:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-07-15 18:38:40 +00:00
|
|
|
|
|
|
|
# (c) 2012, Red Hat, inc
|
|
|
|
# Written by Seth Vidal
|
|
|
|
# based on the mount modules from salt and puppet
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2012-09-30 10:21:35 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: mount
|
|
|
|
short_description: Control active and configured mount points
|
|
|
|
description:
|
|
|
|
- This module controls active and configured mount points in C(/etc/fstab).
|
|
|
|
version_added: "0.6"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- "path to the mount point, eg: C(/mnt/files)"
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
src:
|
|
|
|
description:
|
|
|
|
- device to be mounted on I(name).
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
fstype:
|
|
|
|
description:
|
|
|
|
- file-system type
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
opts:
|
|
|
|
description:
|
|
|
|
- mount options (see fstab(8))
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
dump:
|
|
|
|
description:
|
|
|
|
- dump (see fstab(8))
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
passno:
|
|
|
|
description:
|
|
|
|
- passno (see fstab(8))
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- If C(mounted) or C(unmounted), the device will be actively mounted or unmounted
|
|
|
|
as well as just configured in I(fstab). C(absent) and C(present) only deal with
|
|
|
|
I(fstab).
|
|
|
|
required: true
|
|
|
|
choices: [ "present", "absent", "mounted", "unmounted" ]
|
|
|
|
default: null
|
|
|
|
examples:
|
2012-10-23 13:49:47 +00:00
|
|
|
- code: "mount: name=/mnt/dvd src=/dev/sr0 fstype=iso9660 opts=ro state=present"
|
2012-09-30 10:21:35 +00:00
|
|
|
description: "Mount DVD read-only"
|
|
|
|
notes: []
|
|
|
|
requirements: []
|
|
|
|
author: Seth Vidal
|
|
|
|
'''
|
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
|
|
|
|
def write_fstab(lines, dest):
|
|
|
|
|
|
|
|
fs_w = open(dest, 'w')
|
|
|
|
for l in lines:
|
|
|
|
fs_w.write(l)
|
|
|
|
|
|
|
|
fs_w.flush()
|
|
|
|
fs_w.close()
|
|
|
|
|
|
|
|
def set_mount(**kwargs):
|
2012-07-31 01:50:32 +00:00
|
|
|
""" set/change a mount point location in fstab """
|
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
2012-07-31 01:50:32 +00:00
|
|
|
args = dict(
|
|
|
|
opts = 'defaults',
|
|
|
|
dump = '0',
|
|
|
|
passno = '0',
|
|
|
|
fstab = '/etc/fstab'
|
|
|
|
)
|
2012-07-15 18:38:40 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
|
2012-08-07 00:07:02 +00:00
|
|
|
new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'
|
2012-07-15 18:38:40 +00:00
|
|
|
|
|
|
|
to_write = []
|
|
|
|
exists = False
|
|
|
|
changed = False
|
|
|
|
for line in open(args['fstab'], 'r').readlines():
|
|
|
|
if not line.strip():
|
2012-08-11 16:35:58 +00:00
|
|
|
to_write.append(line)
|
|
|
|
continue
|
2012-07-15 18:38:40 +00:00
|
|
|
if line.strip().startswith('#'):
|
|
|
|
to_write.append(line)
|
|
|
|
continue
|
|
|
|
if len(line.split()) != 6:
|
|
|
|
# not sure what this is or why it is here
|
|
|
|
# but it is not our fault so leave it be
|
|
|
|
to_write.append(line)
|
|
|
|
continue
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
ld = {}
|
|
|
|
ld['src'], ld['name'], ld['fstype'], ld['opts'], ld['dump'], ld['passno'] = line.split()
|
|
|
|
|
|
|
|
if ld['name'] != args['name']:
|
|
|
|
to_write.append(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# it exists - now see if what we have is different
|
|
|
|
exists = True
|
|
|
|
for t in ('src', 'fstype','opts', 'dump', 'passno'):
|
|
|
|
if ld[t] != args[t]:
|
|
|
|
changed = True
|
|
|
|
ld[t] = args[t]
|
|
|
|
|
|
|
|
if changed:
|
2012-08-11 16:35:58 +00:00
|
|
|
to_write.append(new_line % ld)
|
2012-07-15 18:38:40 +00:00
|
|
|
else:
|
2012-08-11 16:35:58 +00:00
|
|
|
to_write.append(line)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
if not exists:
|
|
|
|
to_write.append(new_line % args)
|
|
|
|
changed = True
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
if changed:
|
|
|
|
write_fstab(to_write, args['fstab'])
|
|
|
|
|
|
|
|
return (args['name'], changed)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
|
|
|
|
def unset_mount(**kwargs):
|
2012-07-31 01:50:32 +00:00
|
|
|
""" remove a mount point from fstab """
|
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
2012-07-31 01:50:32 +00:00
|
|
|
args = dict(
|
|
|
|
opts = 'default',
|
|
|
|
dump = '0',
|
|
|
|
passno = '0',
|
|
|
|
fstab = '/etc/fstab'
|
|
|
|
)
|
2012-07-15 18:38:40 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
|
|
|
|
to_write = []
|
|
|
|
changed = False
|
|
|
|
for line in open(args['fstab'], 'r').readlines():
|
|
|
|
if not line.strip():
|
2012-08-11 16:35:58 +00:00
|
|
|
to_write.append(line)
|
|
|
|
continue
|
2012-07-15 18:38:40 +00:00
|
|
|
if line.strip().startswith('#'):
|
|
|
|
to_write.append(line)
|
|
|
|
continue
|
|
|
|
if len(line.split()) != 6:
|
|
|
|
# not sure what this is or why it is here
|
|
|
|
# but it is not our fault so leave it be
|
|
|
|
to_write.append(line)
|
|
|
|
continue
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
ld = {}
|
|
|
|
ld['src'], ld['name'], ld['fstype'], ld['opts'], ld['dump'], ld['passno'] = line.split()
|
|
|
|
|
|
|
|
if ld['name'] != args['name']:
|
|
|
|
to_write.append(line)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# if we got here we found a match - continue and mark changed
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if changed:
|
|
|
|
write_fstab(to_write, args['fstab'])
|
|
|
|
|
|
|
|
return (args['name'], changed)
|
|
|
|
|
2012-08-07 00:07:02 +00:00
|
|
|
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
def mount(module, **kwargs):
|
2012-07-31 01:50:32 +00:00
|
|
|
""" mount up a path or remount if needed """
|
|
|
|
|
2012-07-15 18:38:40 +00:00
|
|
|
name = kwargs['name']
|
|
|
|
if os.path.ismount(name):
|
2012-08-11 16:35:58 +00:00
|
|
|
cmd = [ '/bin/mount', '-o', 'remount', name ]
|
2012-07-15 18:38:40 +00:00
|
|
|
else:
|
2012-08-11 16:35:58 +00:00
|
|
|
cmd = [ '/bin/mount', name ]
|
2012-07-15 18:38:40 +00:00
|
|
|
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc == 0:
|
2012-07-15 18:38:40 +00:00
|
|
|
return 0, ''
|
|
|
|
else:
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
return rc, out+err
|
2012-07-15 18:38:40 +00:00
|
|
|
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
def umount(module, **kwargs):
|
2012-07-31 01:50:32 +00:00
|
|
|
""" unmount a path """
|
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
name = kwargs['name']
|
2012-07-15 18:38:40 +00:00
|
|
|
cmd = ['/bin/umount', name]
|
|
|
|
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc == 0:
|
2012-07-15 18:38:40 +00:00
|
|
|
return 0, ''
|
|
|
|
else:
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
return rc, out+err
|
2012-07-24 11:30:08 +00:00
|
|
|
|
|
|
|
def main():
|
2012-07-31 01:50:32 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
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=None)
|
|
|
|
)
|
|
|
|
)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
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']
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
# 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
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
state = module.params['state']
|
|
|
|
name = module.params['name']
|
|
|
|
if state == 'absent':
|
|
|
|
name, changed = unset_mount(**args)
|
|
|
|
if changed:
|
|
|
|
if os.path.ismount(name):
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
res,msg = umount(module, **args)
|
2012-07-24 11:30:08 +00:00
|
|
|
if res:
|
2012-08-13 19:20:36 +00:00
|
|
|
module.fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
if os.path.exists(name):
|
|
|
|
try:
|
|
|
|
os.rmdir(name)
|
|
|
|
except (OSError, IOError), e:
|
2012-08-13 19:20:36 +00:00
|
|
|
module.fail_json(msg="Error rmdir %s: %s" % (name, str(e)))
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
module.exit_json(changed=changed, **args)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
if state == 'unmounted':
|
2012-07-15 18:38:40 +00:00
|
|
|
if os.path.ismount(name):
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
res,msg = umount(module, **args)
|
2012-07-15 18:38:40 +00:00
|
|
|
if res:
|
2012-08-13 19:20:36 +00:00
|
|
|
module.fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
2012-07-24 11:30:08 +00:00
|
|
|
changed = True
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
module.exit_json(changed=changed, **args)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
if state in ['mounted', 'present']:
|
|
|
|
name, changed = set_mount(**args)
|
|
|
|
if state == 'mounted':
|
|
|
|
if not os.path.exists(name):
|
|
|
|
try:
|
|
|
|
os.makedirs(name)
|
|
|
|
except (OSError, IOError), e:
|
2012-08-13 19:20:36 +00:00
|
|
|
module.fail_json(msg="Error making dir %s: %s" % (name, str(e)))
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
res = 0
|
|
|
|
if os.path.ismount(name):
|
|
|
|
if changed:
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
res,msg = mount(module, **args)
|
2012-07-24 11:30:08 +00:00
|
|
|
else:
|
|
|
|
changed = True
|
Update modules to use run_command in module_common.py
This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
2013-01-12 06:10:21 +00:00
|
|
|
res,msg = mount(module, **args)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
if res:
|
2012-08-13 19:20:36 +00:00
|
|
|
module.fail_json(msg="Error mounting %s: %s" % (name, msg))
|
2012-08-07 00:07:02 +00:00
|
|
|
|
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
module.exit_json(changed=changed, **args)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
module.fail_json(msg='Unexpected position reached')
|
|
|
|
sys.exit(0)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 11:30:08 +00:00
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
|
|
|
main()
|