1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

zpool_facts - pythonification (#3332)

* pythonification

* added changelog fragment

* adjustment per PR
This commit is contained in:
Alexei Znamensky 2021-09-07 05:23:33 +12:00 committed by GitHub
parent 76317d1f64
commit 0f9311c3d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 29 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- zpool_facts - minor refactoring (https://github.com/ansible-collections/community.general/pull/3332).

View file

@ -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