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): def __init__(self, module):
self.module = module self.module = module
self.name = module.params['name'] self.name = module.params['name']
self.parsable = module.params['parsable'] self.parsable = module.params['parsable']
self.properties = module.params['properties'] self.properties = module.params['properties']
self._pools = defaultdict(dict) self._pools = defaultdict(dict)
self.facts = [] self.facts = []
def pool_exists(self): def pool_exists(self):
cmd = [self.module.get_bin_path('zpool'), 'list', self.name] cmd = [self.module.get_bin_path('zpool'), 'list', self.name]
rc, dummy, dummy = self.module.run_command(cmd)
(rc, out, err) = self.module.run_command(cmd) return rc == 0
if rc == 0:
return True
else:
return False
def get_facts(self): def get_facts(self):
cmd = [self.module.get_bin_path('zpool'), 'get', '-H'] cmd = [self.module.get_bin_path('zpool'), 'get', '-H']
@ -153,41 +146,36 @@ class ZPoolFacts(object):
if self.name: if self.name:
cmd.append(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(): for line in out.splitlines():
pool, property, value = line.split('\t') 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): for k, v in iteritems(self._pools):
v.update({'name': k}) v.update({'name': k})
self.facts.append(v) self.facts.append(v)
return {'ansible_zfs_pools': self.facts} 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)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
name=dict(required=False, aliases=['pool', 'zpool'], type='str'), name=dict(aliases=['pool', 'zpool'], type='str'),
parsable=dict(required=False, default=False, type='bool'), parsable=dict(default=False, type='bool'),
properties=dict(required=False, default='all', type='str'), properties=dict(default='all', type='str'),
), ),
supports_check_mode=True supports_check_mode=True
) )
zpool_facts = ZPoolFacts(module) zpool_facts = ZPoolFacts(module)
result = {} result = {
result['changed'] = False 'changed': False,
result['name'] = zpool_facts.name 'name': zpool_facts.name,
}
if zpool_facts.parsable: if zpool_facts.parsable:
result['parsable'] = zpool_facts.parsable result['parsable'] = zpool_facts.parsable