2012-07-15 20:38:40 +02:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-07-15 20:38:40 +02: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/>.
|
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils.basic import get_platform
|
|
|
|
from ansible.module_utils.ismount import ismount
|
|
|
|
from ansible.module_utils.pycompat24 import get_exception
|
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
2012-09-30 12:21:35 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: mount
|
|
|
|
short_description: Control active and configured mount points
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- This module controls active and configured mount points in C(/etc/fstab).
|
|
|
|
author:
|
|
|
|
- Ansible Core Team
|
|
|
|
- Seth Vidal
|
2012-09-30 12:21:35 +02:00
|
|
|
version_added: "0.6"
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Path to the mount point (e.g. C(/mnt/files))
|
2012-09-30 12:21:35 +02:00
|
|
|
required: true
|
|
|
|
src:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Device to be mounted on I(name). Required when I(state) set to
|
|
|
|
C(present) or C(mounted).
|
2016-05-23 17:20:10 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-30 12:21:35 +02:00
|
|
|
fstype:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Filesystem type. Required when I(state) is C(present) or C(mounted).
|
2016-05-23 17:20:10 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
2012-09-30 12:21:35 +02:00
|
|
|
opts:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Mount options (see fstab(5), or vfstab(4) on Solaris).
|
2012-09-30 12:21:35 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
dump:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Dump (see fstab(5)). Note that if set to C(null) and I(state) set to
|
|
|
|
C(present), it will cease to work and duplicate entries will be made
|
|
|
|
with subsequent runs.
|
2016-09-12 14:45:32 +02:00
|
|
|
- Has no effect on Solaris systems.
|
2012-09-30 12:21:35 +02:00
|
|
|
required: false
|
2015-03-12 02:36:44 +01:00
|
|
|
default: 0
|
2012-09-30 12:21:35 +02:00
|
|
|
passno:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Passno (see fstab(5)). Note that if set to C(null) and I(state) set to
|
|
|
|
C(present), it will cease to work and duplicate entries will be made
|
|
|
|
with subsequent runs.
|
2016-09-12 14:45:32 +02:00
|
|
|
- Deprecated on Solaris systems.
|
2012-09-30 12:21:35 +02:00
|
|
|
required: false
|
2015-03-12 02:36:44 +01:00
|
|
|
default: 0
|
2012-09-30 12:21:35 +02:00
|
|
|
state:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- If C(mounted) or C(unmounted), the device will be actively mounted or
|
|
|
|
unmounted as needed and appropriately configured in I(fstab).
|
|
|
|
- C(absent) and C(present) only deal with I(fstab) but will not affect
|
|
|
|
current mounting.
|
|
|
|
- If specifying C(mounted) and the mount point is not present, the mount
|
|
|
|
point will be created. Similarly.
|
2016-03-09 16:05:41 +01:00
|
|
|
- Specifying C(absent) will remove the mount point directory.
|
2012-09-30 12:21:35 +02:00
|
|
|
required: true
|
2016-09-23 21:36:33 +02:00
|
|
|
choices: ["present", "absent", "mounted", "unmounted"]
|
2014-07-21 18:30:59 +02:00
|
|
|
fstab:
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- File to use instead of C(/etc/fstab). You shouldn't use that option
|
2014-07-21 18:30:59 +02:00
|
|
|
unless you really know what you are doing. This might be useful if
|
|
|
|
you need to configure mountpoints in a chroot environment.
|
|
|
|
required: false
|
2016-09-12 14:45:32 +02:00
|
|
|
default: /etc/fstab (/etc/vfstab on Solaris)
|
|
|
|
boot:
|
|
|
|
version_added: 2.2
|
|
|
|
description:
|
2016-09-23 21:36:33 +02:00
|
|
|
- Determines if the filesystem should be mounted on boot.
|
|
|
|
- Only applies to Solaris systems.
|
2016-09-12 14:45:32 +02:00
|
|
|
required: false
|
|
|
|
default: yes
|
2016-09-23 21:36:33 +02:00
|
|
|
choices: ["yes", "no"]
|
2012-09-30 12:21:35 +02:00
|
|
|
'''
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Mount DVD read-only
|
|
|
|
mount:
|
|
|
|
name: /mnt/dvd
|
|
|
|
src: /dev/sr0
|
|
|
|
fstype: iso9660
|
|
|
|
opts: ro
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Mount up device by label
|
|
|
|
mount:
|
|
|
|
name: /srv/disk
|
|
|
|
src: LABEL=SOME_LABEL
|
|
|
|
fstype: ext4
|
|
|
|
state: present
|
|
|
|
|
|
|
|
- name: Mount up device by UUID
|
|
|
|
mount:
|
|
|
|
name: /home
|
|
|
|
src: UUID=b3e48f45-f933-4c8e-a700-22a159ec9077
|
|
|
|
fstype: xfs
|
|
|
|
opts: noatime
|
|
|
|
state: present
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
2012-09-30 12:21:35 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
|
|
|
|
def write_fstab(lines, dest):
|
|
|
|
fs_w = open(dest, 'w')
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
for l in lines:
|
|
|
|
fs_w.write(l)
|
|
|
|
|
|
|
|
fs_w.flush()
|
|
|
|
fs_w.close()
|
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2015-06-02 15:14:07 +02:00
|
|
|
def _escape_fstab(v):
|
2016-09-23 21:36:33 +02:00
|
|
|
"""Escape invalid characters in fstab fields.
|
|
|
|
|
|
|
|
space (040)
|
|
|
|
ampersand (046)
|
|
|
|
backslash (134)
|
|
|
|
"""
|
|
|
|
|
2016-04-25 16:09:19 +02:00
|
|
|
if isinstance(v, int):
|
|
|
|
return v
|
|
|
|
else:
|
2016-09-23 21:36:33 +02:00
|
|
|
return(
|
|
|
|
v.
|
|
|
|
replace('\\', '\\134').
|
|
|
|
replace(' ', '\\040').
|
|
|
|
replace('&', '\\046'))
|
|
|
|
|
2015-06-02 15:14:07 +02:00
|
|
|
|
2014-10-17 18:23:25 +02:00
|
|
|
def set_mount(module, **kwargs):
|
2016-09-23 21:36:33 +02:00
|
|
|
"""Set/change a mount point location in fstab."""
|
2012-07-31 03:50:32 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# solaris kwargs:
|
|
|
|
# name, src, fstype, opts, boot, passno, state, fstab=/etc/vfstab
|
|
|
|
# linux:
|
|
|
|
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS':
|
|
|
|
args = dict(
|
2016-09-23 21:36:33 +02:00
|
|
|
opts='-',
|
|
|
|
passno='-',
|
|
|
|
fstab='/etc/vfstab',
|
|
|
|
boot='yes'
|
2016-09-12 14:45:32 +02:00
|
|
|
)
|
2016-09-23 21:36:33 +02:00
|
|
|
new_line = (
|
|
|
|
'%(src)s - %(name)s %(fstype)s %(passno)s %(boot)s %(opts)s\n')
|
2016-09-12 14:45:32 +02:00
|
|
|
else:
|
|
|
|
args = dict(
|
2016-09-23 21:36:33 +02:00
|
|
|
opts='defaults',
|
|
|
|
dump='0',
|
|
|
|
passno='0',
|
|
|
|
fstab='/etc/fstab'
|
2016-09-12 14:45:32 +02:00
|
|
|
)
|
2016-09-23 21:36:33 +02:00
|
|
|
new_line = (
|
|
|
|
'%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n')
|
2012-07-15 20:38:40 +02:00
|
|
|
args.update(kwargs)
|
|
|
|
|
|
|
|
to_write = []
|
|
|
|
exists = False
|
|
|
|
changed = False
|
2016-08-31 17:16:35 +02:00
|
|
|
escaped_args = dict([(k, _escape_fstab(v)) for k, v in iteritems(args)])
|
2012-07-15 20:38:40 +02:00
|
|
|
for line in open(args['fstab'], 'r').readlines():
|
|
|
|
if not line.strip():
|
2012-08-11 18:35:58 +02:00
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
continue
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
if line.strip().startswith('#'):
|
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
continue
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2016-09-12 14:45:32 +02:00
|
|
|
if len(line.split()) != 6 and get_platform() != 'SunOS':
|
2016-09-23 21:36:33 +02:00
|
|
|
# Not sure what this is or why it is here but it is not our fault
|
|
|
|
# so leave it be
|
2012-07-15 20:38:40 +02:00
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
continue
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
ld = {}
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS':
|
2016-09-23 21:36:33 +02:00
|
|
|
(
|
|
|
|
ld['src'],
|
|
|
|
dash,
|
|
|
|
ld['name'],
|
|
|
|
ld['fstype'],
|
|
|
|
ld['passno'],
|
|
|
|
ld['boot'],
|
|
|
|
ld['opts']
|
|
|
|
) = line.split()
|
2016-09-12 14:45:32 +02:00
|
|
|
else:
|
2016-09-23 21:36:33 +02:00
|
|
|
(
|
|
|
|
ld['src'],
|
|
|
|
ld['name'],
|
|
|
|
ld['fstype'],
|
|
|
|
ld['opts'],
|
|
|
|
ld['dump'],
|
|
|
|
ld['passno']
|
|
|
|
) = line.split()
|
2012-07-15 20:38:40 +02:00
|
|
|
|
2015-06-02 15:14:07 +02:00
|
|
|
if ld['name'] != escaped_args['name']:
|
2012-07-15 20:38:40 +02:00
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
continue
|
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# It exists - now see if what we have is different
|
2012-07-15 20:38:40 +02:00
|
|
|
exists = True
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS':
|
2016-09-23 21:36:33 +02:00
|
|
|
for t in ('src', 'fstype', 'passno', 'boot', 'opts'):
|
2016-09-12 14:45:32 +02:00
|
|
|
if ld[t] != escaped_args[t]:
|
|
|
|
changed = True
|
|
|
|
ld[t] = escaped_args[t]
|
|
|
|
else:
|
2016-09-23 21:36:33 +02:00
|
|
|
for t in ('src', 'fstype', 'opts', 'dump', 'passno'):
|
2016-09-12 14:45:32 +02:00
|
|
|
if ld[t] != escaped_args[t]:
|
|
|
|
changed = True
|
|
|
|
ld[t] = escaped_args[t]
|
2012-07-15 20:38:40 +02:00
|
|
|
|
|
|
|
if changed:
|
2012-08-11 18:35:58 +02:00
|
|
|
to_write.append(new_line % ld)
|
2012-07-15 20:38:40 +02:00
|
|
|
else:
|
2012-08-11 18:35:58 +02:00
|
|
|
to_write.append(line)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
if not exists:
|
2016-04-25 16:09:19 +02:00
|
|
|
to_write.append(new_line % escaped_args)
|
2012-07-15 20:38:40 +02:00
|
|
|
changed = True
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2014-10-17 18:23:25 +02:00
|
|
|
if changed and not module.check_mode:
|
2012-07-15 20:38:40 +02:00
|
|
|
write_fstab(to_write, args['fstab'])
|
|
|
|
|
2016-04-25 16:09:19 +02:00
|
|
|
return (args['name'], changed)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
|
2014-10-17 18:23:25 +02:00
|
|
|
def unset_mount(module, **kwargs):
|
2016-09-23 21:36:33 +02:00
|
|
|
"""Remove a mount point from fstab."""
|
2012-07-31 03:50:32 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# solaris kwargs:
|
|
|
|
# name, src, fstype, opts, boot, passno, state, fstab=/etc/vfstab
|
|
|
|
# linux kwargs:
|
|
|
|
# name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS':
|
|
|
|
args = dict(
|
2016-09-23 21:36:33 +02:00
|
|
|
opts='-',
|
|
|
|
passno='-',
|
|
|
|
fstab='/etc/vfstab',
|
|
|
|
boot='yes'
|
2016-09-12 14:45:32 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
args = dict(
|
2016-09-23 21:36:33 +02:00
|
|
|
opts='default',
|
|
|
|
dump='0',
|
|
|
|
passno='0',
|
|
|
|
fstab='/etc/fstab'
|
2016-09-12 14:45:32 +02:00
|
|
|
)
|
2012-07-15 20:38:40 +02:00
|
|
|
args.update(kwargs)
|
|
|
|
|
|
|
|
to_write = []
|
|
|
|
changed = False
|
2015-06-02 15:14:07 +02:00
|
|
|
escaped_name = _escape_fstab(args['name'])
|
2012-07-15 20:38:40 +02:00
|
|
|
for line in open(args['fstab'], 'r').readlines():
|
|
|
|
if not line.strip():
|
2012-08-11 18:35:58 +02:00
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
continue
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
if line.strip().startswith('#'):
|
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
continue
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2016-09-12 14:45:32 +02:00
|
|
|
if len(line.split()) != 6 and get_platform() != 'SunOS':
|
2016-09-23 21:36:33 +02:00
|
|
|
# Not sure what this is or why it is here but it is not our fault
|
|
|
|
# so leave it be
|
2012-07-15 20:38:40 +02:00
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
continue
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
ld = {}
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS':
|
2016-09-23 21:36:33 +02:00
|
|
|
(
|
|
|
|
ld['src'],
|
|
|
|
dash,
|
|
|
|
ld['name'],
|
|
|
|
ld['fstype'],
|
|
|
|
ld['passno'],
|
|
|
|
ld['boot'],
|
|
|
|
ld['opts']
|
|
|
|
) = line.split()
|
2016-09-12 14:45:32 +02:00
|
|
|
else:
|
2016-09-23 21:36:33 +02:00
|
|
|
(
|
|
|
|
ld['src'],
|
|
|
|
ld['name'],
|
|
|
|
ld['fstype'],
|
|
|
|
ld['opts'],
|
|
|
|
ld['dump'],
|
|
|
|
ld['passno']
|
|
|
|
) = line.split()
|
2012-07-15 20:38:40 +02:00
|
|
|
|
2015-06-02 15:14:07 +02:00
|
|
|
if ld['name'] != escaped_name:
|
2012-07-15 20:38:40 +02:00
|
|
|
to_write.append(line)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2012-07-15 20:38:40 +02:00
|
|
|
continue
|
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# If we got here we found a match - continue and mark changed
|
2012-07-15 20:38:40 +02:00
|
|
|
changed = True
|
|
|
|
|
2014-10-17 18:23:25 +02:00
|
|
|
if changed and not module.check_mode:
|
2012-07-15 20:38:40 +02:00
|
|
|
write_fstab(to_write, args['fstab'])
|
|
|
|
|
2016-04-25 16:09:19 +02:00
|
|
|
return (args['name'], changed)
|
2012-07-15 20:38:40 +02:00
|
|
|
|
2012-08-07 02:07:02 +02: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 07:10:21 +01:00
|
|
|
def mount(module, **kwargs):
|
2016-09-23 21:36:33 +02:00
|
|
|
"""Mount up a path or remount if needed."""
|
2015-05-08 11:22:37 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# solaris kwargs:
|
|
|
|
# name, src, fstype, opts, boot, passno, state, fstab=/etc/vfstab
|
|
|
|
# linux kwargs:
|
|
|
|
# name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS':
|
|
|
|
args = dict(
|
2016-09-23 21:36:33 +02:00
|
|
|
opts='-',
|
|
|
|
passno='-',
|
|
|
|
fstab='/etc/vfstab',
|
|
|
|
boot='yes'
|
2016-09-12 14:45:32 +02:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
args = dict(
|
2016-09-23 21:36:33 +02:00
|
|
|
opts='default',
|
|
|
|
dump='0',
|
|
|
|
passno='0',
|
|
|
|
fstab='/etc/fstab'
|
2016-09-12 14:45:32 +02:00
|
|
|
)
|
2015-05-08 11:22:37 +02:00
|
|
|
args.update(kwargs)
|
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
mount_bin = module.get_bin_path('mount', required=True)
|
2012-07-15 20:38:40 +02:00
|
|
|
name = kwargs['name']
|
2016-09-23 21:36:33 +02:00
|
|
|
cmd = [mount_bin]
|
2016-04-12 17:48:42 +02:00
|
|
|
|
|
|
|
if ismount(name):
|
2016-09-23 21:36:33 +02:00
|
|
|
cmd += ['-o', 'remount']
|
2015-05-08 11:22:37 +02:00
|
|
|
|
|
|
|
if get_platform().lower() == 'freebsd':
|
2016-09-23 21:36:33 +02:00
|
|
|
cmd += ['-F', args['fstab']]
|
2016-09-23 23:31:06 +02:00
|
|
|
elif get_platform().lower() == 'linux' and args['fstab'] != '/etc/fstab':
|
2016-09-23 21:36:33 +02:00
|
|
|
cmd += ['-T', args['fstab']]
|
2015-05-08 11:22:37 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
cmd += [name]
|
2012-07-15 20:38:40 +02: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 07:10:21 +01:00
|
|
|
rc, out, err = module.run_command(cmd)
|
2016-09-23 21:36:33 +02: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 07:10:21 +01:00
|
|
|
if rc == 0:
|
2012-07-15 20:38:40 +02: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 07:10:21 +01:00
|
|
|
return rc, out+err
|
2012-07-15 20:38:40 +02:00
|
|
|
|
2016-09-23 21:36:33 +02: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 07:10:21 +01:00
|
|
|
def umount(module, **kwargs):
|
2016-09-23 21:36:33 +02:00
|
|
|
"""Unmount a path."""
|
2012-07-31 03:50:32 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
umount_bin = module.get_bin_path('umount', required=True)
|
2012-07-24 13:30:08 +02:00
|
|
|
name = kwargs['name']
|
2013-06-17 23:01:35 +02:00
|
|
|
cmd = [umount_bin, name]
|
2012-07-15 20:38:40 +02: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 07:10:21 +01:00
|
|
|
rc, out, err = module.run_command(cmd)
|
2016-09-23 21:36:33 +02: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 07:10:21 +01:00
|
|
|
if rc == 0:
|
2012-07-15 20:38:40 +02: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 07:10:21 +01:00
|
|
|
return rc, out+err
|
2012-07-24 13:30:08 +02:00
|
|
|
|
2012-07-31 03:50:32 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# Note if we wanted to put this into module_utils we'd have to get permission
|
|
|
|
# from @jupeter -- https://github.com/ansible/ansible-modules-core/pull/2923
|
|
|
|
# @jtyr -- https://github.com/ansible/ansible-modules-core/issues/4439
|
|
|
|
# and @abadger to relicense from GPLv3+
|
|
|
|
def is_bind_mounted(module, dest, src=None, fstype=None):
|
|
|
|
"""Return whether the dest is bind mounted
|
|
|
|
|
|
|
|
:arg module: The AnsibleModule (used for helper functions)
|
|
|
|
:arg dest: The directory to be mounted under. This is the primary means
|
|
|
|
of identifying whether the destination is mounted.
|
|
|
|
:kwarg src: The source directory. If specified, this is used to help
|
|
|
|
ensure that we are detecting that the correct source is mounted there.
|
|
|
|
:kwarg fstype: The filesystem type. If specified this is also used to
|
|
|
|
help ensure that we are detecting the right mount.
|
|
|
|
:returns: True if the dest is mounted with src otherwise False.
|
|
|
|
"""
|
|
|
|
|
|
|
|
is_mounted = False
|
|
|
|
bin_path = module.get_bin_path('mount', required=True)
|
|
|
|
cmd = '%s -l' % bin_path
|
|
|
|
|
|
|
|
if get_platform().lower() == 'linux':
|
|
|
|
bin_path = module.get_bin_path('findmnt', required=True)
|
|
|
|
cmd = '%s -nr %s' % (bin_path, dest)
|
|
|
|
|
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
mounts = []
|
|
|
|
|
|
|
|
if len(out):
|
|
|
|
mounts = out.strip().split('\n')
|
|
|
|
|
|
|
|
mount_pattern = re.compile('\[(.*)\]')
|
|
|
|
|
|
|
|
for mnt in mounts:
|
|
|
|
arguments = mnt.split()
|
|
|
|
|
|
|
|
if get_platform().lower() == 'linux':
|
|
|
|
result = mount_pattern.search(arguments[1])
|
|
|
|
|
|
|
|
if len(result.groups()) == 1:
|
|
|
|
if arguments[0] == dest:
|
|
|
|
is_mounted = True
|
|
|
|
elif (
|
|
|
|
(arguments[0] == src or src is None) and
|
|
|
|
arguments[2] == dest and
|
|
|
|
(arguments[4] == fstype or fstype is None)):
|
|
|
|
is_mounted = True
|
|
|
|
|
|
|
|
if is_mounted:
|
|
|
|
break
|
|
|
|
|
|
|
|
return is_mounted
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2012-07-24 13:30:08 +02:00
|
|
|
module = AnsibleModule(
|
2016-09-23 21:36:33 +02:00
|
|
|
argument_spec=dict(
|
|
|
|
boot=dict(default='yes', choices=['yes', 'no']),
|
|
|
|
dump=dict(),
|
|
|
|
fstab=dict(default='/etc/fstab'),
|
|
|
|
fstype=dict(),
|
|
|
|
name=dict(required=True, type='path'),
|
|
|
|
opts=dict(),
|
|
|
|
passno=dict(type='str'),
|
|
|
|
src=dict(type='path'),
|
|
|
|
state=dict(
|
|
|
|
required=True,
|
|
|
|
choices=['present', 'absent', 'mounted', 'unmounted']),
|
2014-10-17 18:23:25 +02:00
|
|
|
),
|
2016-05-23 17:20:10 +02:00
|
|
|
supports_check_mode=True,
|
2016-09-23 21:36:33 +02:00
|
|
|
required_if=(
|
2016-05-23 17:20:10 +02:00
|
|
|
['state', 'mounted', ['src', 'fstype']],
|
|
|
|
['state', 'present', ['src', 'fstype']]
|
|
|
|
)
|
2012-07-24 13:30:08 +02:00
|
|
|
)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 13:30:08 +02:00
|
|
|
changed = False
|
2016-09-23 21:36:33 +02:00
|
|
|
args = {
|
|
|
|
'name': module.params['name']
|
|
|
|
}
|
|
|
|
|
2016-05-23 17:20:10 +02:00
|
|
|
if module.params['src'] is not None:
|
|
|
|
args['src'] = module.params['src']
|
|
|
|
if module.params['fstype'] is not None:
|
|
|
|
args['fstype'] = module.params['fstype']
|
2012-07-24 13:30:08 +02:00
|
|
|
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']
|
2016-09-12 14:45:32 +02:00
|
|
|
if get_platform() == 'SunOS' and module.params['fstab'] == '/etc/fstab':
|
|
|
|
args['fstab'] = '/etc/vfstab'
|
|
|
|
elif module.params['fstab'] is not None:
|
2012-07-24 13:30:08 +02:00
|
|
|
args['fstab'] = module.params['fstab']
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
# If fstab file does not exist, we first need to create it. This mainly
|
|
|
|
# happens when fstab option is passed to the module.
|
2014-07-21 18:32:02 +02:00
|
|
|
if not os.path.exists(args['fstab']):
|
|
|
|
if not os.path.exists(os.path.dirname(args['fstab'])):
|
|
|
|
os.makedirs(os.path.dirname(args['fstab']))
|
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
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.
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 13:30:08 +02:00
|
|
|
state = module.params['state']
|
2016-09-23 21:36:33 +02:00
|
|
|
name = module.params['name']
|
|
|
|
|
2012-07-24 13:30:08 +02:00
|
|
|
if state == 'absent':
|
2014-10-17 18:23:25 +02:00
|
|
|
name, changed = unset_mount(module, **args)
|
2016-09-23 21:36:33 +02:00
|
|
|
|
2014-10-17 18:23:25 +02:00
|
|
|
if changed and not module.check_mode:
|
2016-09-23 21:36:33 +02:00
|
|
|
if ismount(name) or is_bind_mounted(module, name):
|
|
|
|
res, msg = umount(module, **args)
|
|
|
|
|
2012-07-24 13:30:08 +02:00
|
|
|
if res:
|
2016-09-23 21:36:33 +02:00
|
|
|
module.fail_json(
|
|
|
|
msg="Error unmounting %s: %s" % (name, msg))
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-24 13:30:08 +02:00
|
|
|
if os.path.exists(name):
|
|
|
|
try:
|
|
|
|
os.rmdir(name)
|
2016-05-17 19:22:29 +02:00
|
|
|
except (OSError, IOError):
|
|
|
|
e = get_exception()
|
2012-08-13 21:20:36 +02:00
|
|
|
module.fail_json(msg="Error rmdir %s: %s" % (name, str(e)))
|
2016-09-23 21:36:33 +02:00
|
|
|
elif state == 'unmounted':
|
|
|
|
if ismount(name) or is_bind_mounted(module, name):
|
2014-10-17 18:23:25 +02:00
|
|
|
if not module.check_mode:
|
2016-09-23 21:36:33 +02:00
|
|
|
res, msg = umount(module, **args)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
if res:
|
|
|
|
module.fail_json(
|
|
|
|
msg="Error unmounting %s: %s" % (name, msg))
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
changed = True
|
|
|
|
elif 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)))
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2014-10-17 18:23:25 +02:00
|
|
|
name, changed = set_mount(module, **args)
|
2016-09-23 21:36:33 +02:00
|
|
|
res = 0
|
|
|
|
|
|
|
|
if ismount(name):
|
|
|
|
if not module.check_mode:
|
|
|
|
res, msg = mount(module, **args)
|
2012-07-24 13:30:08 +02:00
|
|
|
changed = True
|
2016-09-23 21:36:33 +02:00
|
|
|
elif 'bind' in args.get('opts', []):
|
|
|
|
changed = True
|
2014-10-17 18:23:25 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
if is_bind_mounted(module, name, args['src'], args['fstype']):
|
|
|
|
changed = False
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
if changed and not module.check_mode:
|
|
|
|
res, msg = mount(module, **args)
|
|
|
|
else:
|
|
|
|
changed = True
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
if not module.check_mode:
|
|
|
|
res, msg = mount(module, **args)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
if res:
|
|
|
|
module.fail_json(msg="Error mounting %s: %s" % (name, msg))
|
|
|
|
elif state == 'present':
|
|
|
|
name, changed = set_mount(module, **args)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='Unexpected position reached')
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
module.exit_json(changed=changed, **args)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2016-04-12 17:48:42 +02:00
|
|
|
|
2016-09-23 21:36:33 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|