2013-02-14 14:16:08 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Johan Wiren <johan.wiren.se@gmail.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: zfs
|
|
|
|
short_description: Manage zfs
|
2013-02-18 01:25:44 +01:00
|
|
|
description:
|
2015-12-06 21:34:55 +01:00
|
|
|
- Manages ZFS file systems, volumes, clones and snapshots.
|
2013-02-18 01:29:51 +01:00
|
|
|
version_added: "1.1"
|
2013-02-14 14:16:08 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- File system, snapshot or volume name e.g. C(rpool/myfs)
|
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
2015-12-10 23:32:13 +01:00
|
|
|
- Whether to create (C(present)), or remove (C(absent)) a
|
|
|
|
file system, snapshot or volume. All parents/children
|
|
|
|
will be created/destroyed as needed to reach the desired state.
|
2015-12-06 21:34:55 +01:00
|
|
|
choices: ['present', 'absent']
|
2013-02-14 14:16:08 +01:00
|
|
|
required: true
|
2015-07-04 01:16:18 +02:00
|
|
|
origin:
|
|
|
|
description:
|
2015-12-06 21:34:55 +01:00
|
|
|
- Snapshot from which to create a clone
|
2015-12-09 00:30:39 +01:00
|
|
|
default: null
|
2015-12-06 21:34:55 +01:00
|
|
|
required: false
|
|
|
|
key_value:
|
2013-02-16 15:11:07 +01:00
|
|
|
description:
|
2015-12-06 21:34:55 +01:00
|
|
|
- The C(zfs) module takes key=value pairs for zfs properties to be set. See the zfs(8) man page for more information.
|
2015-12-09 00:30:39 +01:00
|
|
|
default: null
|
|
|
|
required: false
|
2015-12-06 21:34:55 +01:00
|
|
|
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Johan Wiren (@johanwiren)"
|
2013-02-14 14:16:08 +01:00
|
|
|
'''
|
2013-06-14 11:53:43 +02:00
|
|
|
|
|
|
|
EXAMPLES = '''
|
2015-12-09 00:02:34 +01:00
|
|
|
# Create a new file system called myfs in pool rpool with the setuid property turned off
|
|
|
|
- zfs: name=rpool/myfs state=present setuid=off
|
2013-06-14 11:53:43 +02:00
|
|
|
|
2015-12-06 21:34:55 +01:00
|
|
|
# Create a new volume called myvol in pool rpool.
|
2013-06-14 11:53:43 +02:00
|
|
|
- zfs: name=rpool/myvol state=present volsize=10M
|
|
|
|
|
|
|
|
# Create a snapshot of rpool/myfs file system.
|
|
|
|
- zfs: name=rpool/myfs@mysnapshot state=present
|
|
|
|
|
|
|
|
# Create a new file system called myfs2 with snapdir enabled
|
|
|
|
- zfs: name=rpool/myfs2 state=present snapdir=enabled
|
2015-07-04 01:16:18 +02:00
|
|
|
|
|
|
|
# Create a new file system by cloning a snapshot
|
|
|
|
- zfs: name=rpool/cloned_fs state=present origin=rpool/myfs@mysnapshot
|
|
|
|
|
|
|
|
# Destroy a filesystem
|
|
|
|
- zfs: name=rpool/myfs state=absent
|
2013-06-14 11:53:43 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
import os
|
|
|
|
|
2015-12-06 21:34:55 +01:00
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
class Zfs(object):
|
2015-12-06 21:34:55 +01:00
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
def __init__(self, module, name, properties):
|
|
|
|
self.module = module
|
|
|
|
self.name = name
|
|
|
|
self.properties = properties
|
|
|
|
self.changed = False
|
2015-12-06 21:34:55 +01:00
|
|
|
self.is_solaris = os.uname()[0] == 'SunOS'
|
|
|
|
self.pool = name.split('/')[0]
|
|
|
|
self.zfs_cmd = module.get_bin_path('zfs', True)
|
|
|
|
self.zpool_cmd = module.get_bin_path('zpool', True)
|
|
|
|
self.enhanced_sharing = self.check_enhanced_sharing()
|
|
|
|
|
|
|
|
def check_enhanced_sharing(self):
|
|
|
|
if os.uname()[0] == 'SunOS':
|
|
|
|
cmd = [self.zpool_cmd]
|
|
|
|
cmd.extend(['get', 'version'])
|
|
|
|
cmd.append(self.pool)
|
|
|
|
(rc, out, err) = self.module.run_command(cmd, check_rc=True)
|
|
|
|
version = out.splitlines()[-1].split()[2]
|
|
|
|
if int(version) >= 34:
|
|
|
|
return True
|
|
|
|
return False
|
2013-02-14 14:16:08 +01:00
|
|
|
|
|
|
|
def exists(self):
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd = [self.zfs_cmd, 'list', '-t', 'all', self.name]
|
2013-02-14 14:16:08 +01:00
|
|
|
(rc, out, err) = self.module.run_command(' '.join(cmd))
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def create(self):
|
2013-02-22 15:01:18 +01:00
|
|
|
if self.module.check_mode:
|
|
|
|
self.changed = True
|
|
|
|
return
|
2015-01-22 15:13:12 +01:00
|
|
|
properties = self.properties
|
2013-02-14 14:16:08 +01:00
|
|
|
volsize = properties.pop('volsize', None)
|
|
|
|
volblocksize = properties.pop('volblocksize', None)
|
2015-07-04 01:16:18 +02:00
|
|
|
origin = properties.pop('origin', None)
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd = [self.zfs_cmd]
|
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
if "@" in self.name:
|
|
|
|
action = 'snapshot'
|
2015-07-04 01:16:18 +02:00
|
|
|
elif origin:
|
2015-07-03 02:07:07 +02:00
|
|
|
action = 'clone'
|
2013-02-14 14:16:08 +01:00
|
|
|
else:
|
|
|
|
action = 'create'
|
|
|
|
|
|
|
|
cmd.append(action)
|
2015-01-22 15:13:12 +01:00
|
|
|
|
2015-12-06 21:34:55 +01:00
|
|
|
if action in ['create', 'clone']:
|
2015-12-10 23:32:13 +01:00
|
|
|
cmd += ['-p']
|
2015-01-22 15:13:12 +01:00
|
|
|
|
2015-12-06 21:34:55 +01:00
|
|
|
if volsize:
|
|
|
|
cmd += ['-V', volsize]
|
2013-02-14 14:16:08 +01:00
|
|
|
if volblocksize:
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd += ['-b', 'volblocksize']
|
2013-02-14 14:16:08 +01:00
|
|
|
if properties:
|
|
|
|
for prop, value in properties.iteritems():
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd += ['-o', '%s="%s"' % (prop, value)]
|
2015-07-04 01:16:18 +02:00
|
|
|
if origin:
|
|
|
|
cmd.append(origin)
|
2013-02-14 14:16:08 +01:00
|
|
|
cmd.append(self.name)
|
2015-12-06 21:34:55 +01:00
|
|
|
(rc, out, err) = self.module.run_command(' '.join(cmd))
|
2013-02-14 14:16:08 +01:00
|
|
|
if rc == 0:
|
2015-01-22 15:13:12 +01:00
|
|
|
self.changed = True
|
2013-02-14 14:16:08 +01:00
|
|
|
else:
|
2015-12-06 21:34:55 +01:00
|
|
|
self.module.fail_json(msg=err)
|
2013-02-14 14:16:08 +01:00
|
|
|
|
|
|
|
def destroy(self):
|
2013-02-22 15:01:18 +01:00
|
|
|
if self.module.check_mode:
|
|
|
|
self.changed = True
|
|
|
|
return
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd = [self.zfs_cmd, 'destroy', '-R', self.name]
|
|
|
|
(rc, out, err) = self.module.run_command(' '.join(cmd))
|
2013-02-14 14:16:08 +01:00
|
|
|
if rc == 0:
|
|
|
|
self.changed = True
|
|
|
|
else:
|
2015-12-06 21:34:55 +01:00
|
|
|
self.module.fail_json(msg=err)
|
2013-02-14 14:16:08 +01:00
|
|
|
|
|
|
|
def set_property(self, prop, value):
|
2013-02-22 15:01:18 +01:00
|
|
|
if self.module.check_mode:
|
|
|
|
self.changed = True
|
|
|
|
return
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd = [self.zfs_cmd, 'set', prop + '=' + str(value), self.name]
|
|
|
|
(rc, out, err) = self.module.run_command(cmd)
|
2013-02-14 14:16:08 +01:00
|
|
|
if rc == 0:
|
|
|
|
self.changed = True
|
|
|
|
else:
|
2015-12-06 21:34:55 +01:00
|
|
|
self.module.fail_json(msg=err)
|
2013-02-14 14:16:08 +01:00
|
|
|
|
|
|
|
def set_properties_if_changed(self):
|
|
|
|
current_properties = self.get_current_properties()
|
|
|
|
for prop, value in self.properties.iteritems():
|
2015-12-10 22:53:06 +01:00
|
|
|
if current_properties.get(prop, None) != value:
|
2015-12-06 21:34:55 +01:00
|
|
|
self.set_property(prop, value)
|
2013-02-14 14:16:08 +01:00
|
|
|
|
|
|
|
def get_current_properties(self):
|
2015-12-06 21:34:55 +01:00
|
|
|
cmd = [self.zfs_cmd, 'get', '-H']
|
|
|
|
if self.enhanced_sharing:
|
|
|
|
cmd += ['-e']
|
|
|
|
cmd += ['all', self.name]
|
|
|
|
rc, out, err = self.module.run_command(" ".join(cmd))
|
|
|
|
properties = dict()
|
2015-12-10 22:53:06 +01:00
|
|
|
for prop, value, source in [l.split('\t')[1:4] for l in out.splitlines()]:
|
|
|
|
if source == 'local':
|
|
|
|
properties[prop] = value
|
2015-12-06 21:34:55 +01:00
|
|
|
# Add alias for enhanced sharing properties
|
|
|
|
properties['sharenfs'] = properties.get('share.nfs', None)
|
|
|
|
properties['sharesmb'] = properties.get('share.smb', None)
|
2013-02-14 14:16:08 +01:00
|
|
|
return properties
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2013-06-01 18:20:45 +02:00
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
module = AnsibleModule(
|
2015-12-06 21:34:55 +01:00
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(type='str', required=True),
|
|
|
|
state = dict(type='str', required=True, choices=['present', 'absent']),
|
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
check_invalid_arguments=False
|
2013-02-14 14:16:08 +01:00
|
|
|
)
|
2013-02-22 15:01:18 +01:00
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
state = module.params.pop('state')
|
|
|
|
name = module.params.pop('name')
|
|
|
|
|
2013-02-22 15:01:18 +01:00
|
|
|
# Get all valid zfs-properties
|
2013-02-14 14:16:08 +01:00
|
|
|
properties = dict()
|
|
|
|
for prop, value in module.params.iteritems():
|
2015-12-06 21:34:55 +01:00
|
|
|
# All freestyle params are zfs properties
|
|
|
|
if prop not in module.argument_spec:
|
|
|
|
# Reverse the boolification of freestyle zfs properties
|
|
|
|
if type(value) == bool:
|
|
|
|
if value is True:
|
|
|
|
properties[prop] = 'on'
|
|
|
|
else:
|
|
|
|
properties[prop] = 'off'
|
|
|
|
else:
|
|
|
|
properties[prop] = value
|
2013-02-22 15:01:18 +01:00
|
|
|
|
2013-02-14 14:16:08 +01:00
|
|
|
result = {}
|
|
|
|
result['name'] = name
|
|
|
|
result['state'] = state
|
|
|
|
|
2015-01-22 15:13:12 +01:00
|
|
|
zfs = Zfs(module, name, properties)
|
2013-02-14 14:16:08 +01:00
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
if zfs.exists():
|
|
|
|
zfs.set_properties_if_changed()
|
|
|
|
else:
|
|
|
|
zfs.create()
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
if zfs.exists():
|
|
|
|
zfs.destroy()
|
|
|
|
|
|
|
|
result.update(zfs.properties)
|
|
|
|
result['changed'] = zfs.changed
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2013-12-02 21:11:23 +01:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-02-14 14:16:08 +01:00
|
|
|
main()
|