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:
parent
76317d1f64
commit
0f9311c3d9
2 changed files with 19 additions and 29 deletions
2
changelogs/fragments/3332-zpool_facts-pythonify.yaml
Normal file
2
changelogs/fragments/3332-zpool_facts-pythonify.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- zpool_facts - minor refactoring (https://github.com/ansible-collections/community.general/pull/3332).
|
|
@ -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')
|
||||
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)
|
||||
|
||||
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():
|
||||
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
|
||||
|
||||
|
|
Loading…
Reference in a new issue