From 73de447489859c9d4ef1dfea68ef6da184761b87 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 6 Sep 2021 20:27:45 +0200 Subject: [PATCH] zpool_facts - pythonification (#3332) (#3339) * pythonification * added changelog fragment * adjustment per PR (cherry picked from commit 0f9311c3d9d1d1f6eeff43ff40b5d524555e0fe6) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../fragments/3332-zpool_facts-pythonify.yaml | 2 + plugins/modules/storage/zfs/zpool_facts.py | 46 +++++++------------ 2 files changed, 19 insertions(+), 29 deletions(-) create mode 100644 changelogs/fragments/3332-zpool_facts-pythonify.yaml diff --git a/changelogs/fragments/3332-zpool_facts-pythonify.yaml b/changelogs/fragments/3332-zpool_facts-pythonify.yaml new file mode 100644 index 0000000000..ddb29b9efb --- /dev/null +++ b/changelogs/fragments/3332-zpool_facts-pythonify.yaml @@ -0,0 +1,2 @@ +minor_changes: + - zpool_facts - minor refactoring (https://github.com/ansible-collections/community.general/pull/3332). diff --git a/plugins/modules/storage/zfs/zpool_facts.py b/plugins/modules/storage/zfs/zpool_facts.py index ed3d6cf965..b7a66255c6 100644 --- a/plugins/modules/storage/zfs/zpool_facts.py +++ b/plugins/modules/storage/zfs/zpool_facts.py @@ -125,23 +125,16 @@ class ZPoolFacts(object): def __init__(self, module): self.module = module - self.name = module.params['name'] self.parsable = module.params['parsable'] self.properties = module.params['properties'] - self._pools = defaultdict(dict) self.facts = [] def pool_exists(self): cmd = [self.module.get_bin_path('zpool'), 'list', self.name] - - (rc, out, err) = self.module.run_command(cmd) - - if rc == 0: - return True - else: - return False + rc, dummy, dummy = self.module.run_command(cmd) + return rc == 0 def get_facts(self): cmd = [self.module.get_bin_path('zpool'), 'get', '-H'] @@ -153,41 +146,36 @@ class ZPoolFacts(object): if self.name: cmd.append(self.name) - (rc, out, err) = self.module.run_command(cmd) + rc, out, err = self.module.run_command(cmd, check_rc=True) - if rc == 0: - for line in out.splitlines(): - pool, property, value = line.split('\t') + for line in out.splitlines(): + pool, prop, value = line.split('\t') - self._pools[pool].update({property: value}) + self._pools[pool].update({prop: value}) - for k, v in iteritems(self._pools): - v.update({'name': k}) - self.facts.append(v) + for k, v in iteritems(self._pools): + v.update({'name': k}) + self.facts.append(v) - return {'ansible_zfs_pools': self.facts} - else: - self.module.fail_json(msg='Error while trying to get facts about ZFS pool: %s' % self.name, - stderr=err, - rc=rc) + return {'ansible_zfs_pools': self.facts} def main(): module = AnsibleModule( argument_spec=dict( - name=dict(required=False, aliases=['pool', 'zpool'], type='str'), - parsable=dict(required=False, default=False, type='bool'), - properties=dict(required=False, default='all', type='str'), + name=dict(aliases=['pool', 'zpool'], type='str'), + parsable=dict(default=False, type='bool'), + properties=dict(default='all', type='str'), ), supports_check_mode=True ) zpool_facts = ZPoolFacts(module) - result = {} - result['changed'] = False - result['name'] = zpool_facts.name - + result = { + 'changed': False, + 'name': zpool_facts.name, + } if zpool_facts.parsable: result['parsable'] = zpool_facts.parsable