zfs: refactoring (#4650)

* zfs: refactoring

* added changelog fragment
pull/4415/merge
Alexei Znamensky 2022-05-08 23:50:53 +12:00 committed by GitHub
parent dc4cdb1a58
commit b9266c31d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 22 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- zfs - minor refactoring in the code (https://github.com/ansible-collections/community.general/pull/4650).

View File

@ -130,18 +130,15 @@ class Zfs(object):
def exists(self): def exists(self):
cmd = [self.zfs_cmd, 'list', '-t', 'all', self.name] cmd = [self.zfs_cmd, 'list', '-t', 'all', self.name]
(rc, out, err) = self.module.run_command(' '.join(cmd)) rc, dummy, dummy = self.module.run_command(cmd)
if rc == 0: return rc == 0
return True
else:
return False
def create(self): def create(self):
if self.module.check_mode: if self.module.check_mode:
self.changed = True self.changed = True
return return
properties = self.properties properties = self.properties
origin = self.module.params.get('origin', None) origin = self.module.params.get('origin')
cmd = [self.zfs_cmd] cmd = [self.zfs_cmd]
if "@" in self.name: if "@" in self.name:
@ -167,31 +164,23 @@ class Zfs(object):
if origin and action == 'clone': if origin and action == 'clone':
cmd.append(origin) cmd.append(origin)
cmd.append(self.name) cmd.append(self.name)
(rc, out, err) = self.module.run_command(' '.join(cmd)) self.module.run_command(cmd, check_rc=True)
if rc == 0: self.changed = True
self.changed = True
else:
self.module.fail_json(msg=err)
def destroy(self): def destroy(self):
if self.module.check_mode: if self.module.check_mode:
self.changed = True self.changed = True
return return
cmd = [self.zfs_cmd, 'destroy', '-R', self.name] cmd = [self.zfs_cmd, 'destroy', '-R', self.name]
(rc, out, err) = self.module.run_command(' '.join(cmd)) self.module.run_command(cmd, check_rc=True)
if rc == 0: self.changed = True
self.changed = True
else:
self.module.fail_json(msg=err)
def set_property(self, prop, value): def set_property(self, prop, value):
if self.module.check_mode: if self.module.check_mode:
self.changed = True self.changed = True
return return
cmd = [self.zfs_cmd, 'set', prop + '=' + str(value), self.name] cmd = [self.zfs_cmd, 'set', prop + '=' + str(value), self.name]
(rc, out, err) = self.module.run_command(cmd) self.module.run_command(cmd, check_rc=True)
if rc != 0:
self.module.fail_json(msg=err)
def set_properties_if_changed(self): def set_properties_if_changed(self):
diff = {'before': {'extra_zfs_properties': {}}, 'after': {'extra_zfs_properties': {}}} diff = {'before': {'extra_zfs_properties': {}}, 'after': {'extra_zfs_properties': {}}}
@ -220,14 +209,14 @@ class Zfs(object):
if self.enhanced_sharing: if self.enhanced_sharing:
cmd += ['-e'] cmd += ['-e']
cmd += ['all', self.name] cmd += ['all', self.name]
rc, out, err = self.module.run_command(" ".join(cmd)) rc, out, err = self.module.run_command(cmd)
properties = dict() properties = dict()
for line in out.splitlines(): for line in out.splitlines():
prop, value, source = line.split('\t') prop, value, source = line.split('\t')
# include source '-' so that creation-only properties are not removed # include source '-' so that creation-only properties are not removed
# to avoids errors when the dataset already exists and the property is not changed # to avoids errors when the dataset already exists and the property is not changed
# this scenario is most likely when the same playbook is run more than once # this scenario is most likely when the same playbook is run more than once
if source == 'local' or source == 'received' or source == '-': if source in ('local', 'received', '-'):
properties[prop] = value properties[prop] = value
# Add alias for enhanced sharing properties # Add alias for enhanced sharing properties
if self.enhanced_sharing: if self.enhanced_sharing:
@ -242,7 +231,7 @@ def main():
argument_spec=dict( argument_spec=dict(
name=dict(type='str', required=True), name=dict(type='str', required=True),
state=dict(type='str', required=True, choices=['absent', 'present']), state=dict(type='str', required=True, choices=['absent', 'present']),
origin=dict(type='str', default=None), origin=dict(type='str'),
extra_zfs_properties=dict(type='dict', default={}), extra_zfs_properties=dict(type='dict', default={}),
), ),
supports_check_mode=True, supports_check_mode=True,