1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -95,26 +95,25 @@ from ansible.module_utils.basic import AnsibleModule
class Zfs:
def __init__(self, module, name, extra_zfs_properties):
self.module = module
self.name = name
self.extra_zfs_properties = extra_zfs_properties
self.changed = False
self.zfs_cmd = module.get_bin_path('zfs', True)
self.zpool_cmd = module.get_bin_path('zpool', True)
self.pool = name.split('/')[0].split('@')[0]
self.is_solaris = os.uname()[0] == 'SunOS'
self.zfs_cmd = module.get_bin_path("zfs", True)
self.zpool_cmd = module.get_bin_path("zpool", True)
self.pool = name.split("/")[0].split("@")[0]
self.is_solaris = os.uname()[0] == "SunOS"
self.is_openzfs = self.check_openzfs()
self.enhanced_sharing = self.check_enhanced_sharing()
def check_openzfs(self):
cmd = [self.zpool_cmd]
cmd.extend(['get', 'version'])
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 version == '-':
if version == "-":
return True
if int(version) == 5000:
return True
@ -123,7 +122,7 @@ class Zfs:
def check_enhanced_sharing(self):
if self.is_solaris and not self.is_openzfs:
cmd = [self.zpool_cmd]
cmd.extend(['get', 'version'])
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]
@ -132,7 +131,7 @@ class Zfs:
return False
def exists(self):
cmd = [self.zfs_cmd, 'list', '-t', 'all', self.name]
cmd = [self.zfs_cmd, "list", "-t", "all", self.name]
rc, dummy, dummy = self.module.run_command(cmd)
return rc == 0
@ -141,30 +140,30 @@ class Zfs:
self.changed = True
return
extra_zfs_properties = self.extra_zfs_properties
origin = self.module.params.get('origin')
origin = self.module.params.get("origin")
cmd = [self.zfs_cmd]
if "@" in self.name:
action = 'snapshot'
action = "snapshot"
elif origin:
action = 'clone'
action = "clone"
else:
action = 'create'
action = "create"
cmd.append(action)
if action in ['create', 'clone']:
cmd += ['-p']
if action in ["create", "clone"]:
cmd += ["-p"]
if extra_zfs_properties:
for prop, value in extra_zfs_properties.items():
if prop == 'volsize':
cmd += ['-V', value]
elif prop == 'volblocksize':
cmd += ['-b', value]
if prop == "volsize":
cmd += ["-V", value]
elif prop == "volblocksize":
cmd += ["-b", value]
else:
cmd += ['-o', f'{prop}={value}']
if origin and action == 'clone':
cmd += ["-o", f"{prop}={value}"]
if origin and action == "clone":
cmd.append(origin)
cmd.append(self.name)
self.module.run_command(cmd, check_rc=True)
@ -174,7 +173,7 @@ class Zfs:
if self.module.check_mode:
self.changed = True
return
cmd = [self.zfs_cmd, 'destroy', '-R', self.name]
cmd = [self.zfs_cmd, "destroy", "-R", self.name]
self.module.run_command(cmd, check_rc=True)
self.changed = True
@ -182,18 +181,18 @@ class Zfs:
if self.module.check_mode:
self.changed = True
return
cmd = [self.zfs_cmd, 'set', f"{prop}={value!s}", self.name]
cmd = [self.zfs_cmd, "set", f"{prop}={value!s}", self.name]
self.module.run_command(cmd, check_rc=True)
def set_properties_if_changed(self):
diff = {'before': {'extra_zfs_properties': {}}, 'after': {'extra_zfs_properties': {}}}
diff = {"before": {"extra_zfs_properties": {}}, "after": {"extra_zfs_properties": {}}}
current_properties = self.list_properties()
for prop, value in self.extra_zfs_properties.items():
current_value = self.get_property(prop, current_properties)
if current_value != value:
self.set_property(prop, value)
diff['before']['extra_zfs_properties'][prop] = current_value
diff['after']['extra_zfs_properties'][prop] = value
diff["before"]["extra_zfs_properties"][prop] = current_value
diff["after"]["extra_zfs_properties"][prop] = value
if self.module.check_mode:
return diff
updated_properties = self.list_properties()
@ -203,38 +202,38 @@ class Zfs:
self.module.fail_json(msg=f"zfsprop was not present after being successfully set: {prop}")
if self.get_property(prop, current_properties) != value:
self.changed = True
if prop in diff['after']['extra_zfs_properties']:
diff['after']['extra_zfs_properties'][prop] = value
if prop in diff["after"]["extra_zfs_properties"]:
diff["after"]["extra_zfs_properties"][prop] = value
return diff
def list_properties(self):
cmd = [self.zfs_cmd, 'get', '-H', '-p', '-o', "property,source"]
cmd = [self.zfs_cmd, "get", "-H", "-p", "-o", "property,source"]
if self.enhanced_sharing:
cmd += ['-e']
cmd += ['all', self.name]
cmd += ["-e"]
cmd += ["all", self.name]
rc, out, err = self.module.run_command(cmd)
properties = []
for line in out.splitlines():
prop, source = line.split('\t')
prop, source = line.split("\t")
# include source '-' so that creation-only properties are not removed
# 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
if source in ('local', 'received', '-'):
if source in ("local", "received", "-"):
properties.append(prop)
return properties
def get_property(self, name, list_of_properties):
# Add alias for enhanced sharing properties
if self.enhanced_sharing:
if name == 'sharenfs':
name = 'share.nfs'
elif name == 'sharesmb':
name = 'share.smb'
if name == "sharenfs":
name = "share.nfs"
elif name == "sharesmb":
name = "share.smb"
if name not in list_of_properties:
return None
cmd = [self.zfs_cmd, 'get', '-H', '-p', '-o', "value"]
cmd = [self.zfs_cmd, "get", "-H", "-p", "-o", "value"]
if self.enhanced_sharing:
cmd += ['-e']
cmd += ["-e"]
cmd += [name, self.name]
rc, out, err = self.module.run_command(cmd)
if rc != 0:
@ -246,61 +245,60 @@ class Zfs:
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
state=dict(type='str', required=True, choices=['absent', 'present']),
origin=dict(type='str'),
extra_zfs_properties=dict(type='dict', default={}),
name=dict(type="str", required=True),
state=dict(type="str", required=True, choices=["absent", "present"]),
origin=dict(type="str"),
extra_zfs_properties=dict(type="dict", default={}),
),
supports_check_mode=True,
)
state = module.params.get('state')
name = module.params.get('name')
state = module.params.get("state")
name = module.params.get("name")
if module.params.get('origin') and '@' in name:
module.fail_json(msg='cannot specify origin when operating on a snapshot')
if module.params.get("origin") and "@" in name:
module.fail_json(msg="cannot specify origin when operating on a snapshot")
# Reverse the boolification of zfs properties
for prop, value in module.params['extra_zfs_properties'].items():
for prop, value in module.params["extra_zfs_properties"].items():
if isinstance(value, bool):
if value is True:
module.params['extra_zfs_properties'][prop] = 'on'
module.params["extra_zfs_properties"][prop] = "on"
else:
module.params['extra_zfs_properties'][prop] = 'off'
module.params["extra_zfs_properties"][prop] = "off"
else:
module.params['extra_zfs_properties'][prop] = value
module.params["extra_zfs_properties"][prop] = value
result = dict(
name=name,
state=state,
)
zfs = Zfs(module, name, module.params['extra_zfs_properties'])
zfs = Zfs(module, name, module.params["extra_zfs_properties"])
if state == 'present':
if state == "present":
if zfs.exists():
result['diff'] = zfs.set_properties_if_changed()
result["diff"] = zfs.set_properties_if_changed()
else:
zfs.create()
result['diff'] = {'before': {'state': 'absent'}, 'after': {'state': state}}
result["diff"] = {"before": {"state": "absent"}, "after": {"state": state}}
elif state == 'absent':
elif state == "absent":
if zfs.exists():
zfs.destroy()
result['diff'] = {'before': {'state': 'present'}, 'after': {'state': state}}
result["diff"] = {"before": {"state": "present"}, "after": {"state": state}}
else:
result['diff'] = {}
result["diff"] = {}
result['diff']['before_header'] = name
result['diff']['after_header'] = name
result["diff"]["before_header"] = name
result["diff"]["after_header"] = name
result.update(zfs.extra_zfs_properties)
result['changed'] = zfs.changed
result["changed"] = zfs.changed
module.exit_json(**result)
if __name__ == '__main__':
if __name__ == "__main__":
main()