2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 22:45:15 +02:00
|
|
|
# Copyright (c) 2016, Adam Števko <adam.stevko@gmail.com>
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: zpool_facts
|
|
|
|
short_description: Gather facts about ZFS pools.
|
|
|
|
description:
|
|
|
|
- Gather facts from ZFS pool properties.
|
|
|
|
author: Adam Števko (@xen0l)
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- ZFS pool name.
|
2021-02-15 07:16:08 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
aliases: [ "pool", "zpool" ]
|
|
|
|
required: false
|
|
|
|
parsable:
|
|
|
|
description:
|
|
|
|
- Specifies if property values should be displayed in machine
|
|
|
|
friendly format.
|
|
|
|
type: bool
|
2022-09-06 21:07:46 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
required: false
|
|
|
|
properties:
|
|
|
|
description:
|
|
|
|
- Specifies which dataset properties should be queried in comma-separated format.
|
|
|
|
For more information about dataset properties, check zpool(1M) man page.
|
2021-02-15 07:16:08 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
default: all
|
|
|
|
required: false
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather facts about ZFS pool rpool
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.zpool_facts: pool=rpool
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Gather space usage about all imported ZFS pools
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.zpool_facts: properties='free,size'
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-15 12:13:45 +02:00
|
|
|
- name: Print gathered information
|
2020-07-14 17:28:08 +02:00
|
|
|
ansible.builtin.debug:
|
2020-05-15 12:13:45 +02:00
|
|
|
msg: 'ZFS pool {{ item.name }} has {{ item.free }} free space out of {{ item.size }}.'
|
2020-03-09 10:11:07 +01:00
|
|
|
with_items: '{{ ansible_zfs_pools }}'
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
ansible_facts:
|
|
|
|
description: Dictionary containing all the detailed information about the ZFS pool facts
|
|
|
|
returned: always
|
|
|
|
type: complex
|
|
|
|
contains:
|
|
|
|
ansible_zfs_pools:
|
|
|
|
description: ZFS pool facts
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample:
|
|
|
|
{
|
|
|
|
"allocated": "3.46G",
|
|
|
|
"altroot": "-",
|
|
|
|
"autoexpand": "off",
|
|
|
|
"autoreplace": "off",
|
|
|
|
"bootfs": "rpool/ROOT/openindiana",
|
|
|
|
"cachefile": "-",
|
|
|
|
"capacity": "6%",
|
|
|
|
"comment": "-",
|
|
|
|
"dedupditto": "0",
|
|
|
|
"dedupratio": "1.00x",
|
|
|
|
"delegation": "on",
|
|
|
|
"expandsize": "-",
|
|
|
|
"failmode": "wait",
|
|
|
|
"feature@async_destroy": "enabled",
|
|
|
|
"feature@bookmarks": "enabled",
|
|
|
|
"feature@edonr": "enabled",
|
|
|
|
"feature@embedded_data": "active",
|
|
|
|
"feature@empty_bpobj": "active",
|
|
|
|
"feature@enabled_txg": "active",
|
|
|
|
"feature@extensible_dataset": "enabled",
|
|
|
|
"feature@filesystem_limits": "enabled",
|
|
|
|
"feature@hole_birth": "active",
|
|
|
|
"feature@large_blocks": "enabled",
|
|
|
|
"feature@lz4_compress": "active",
|
|
|
|
"feature@multi_vdev_crash_dump": "enabled",
|
|
|
|
"feature@sha512": "enabled",
|
|
|
|
"feature@skein": "enabled",
|
|
|
|
"feature@spacemap_histogram": "active",
|
|
|
|
"fragmentation": "3%",
|
|
|
|
"free": "46.3G",
|
|
|
|
"freeing": "0",
|
|
|
|
"guid": "15729052870819522408",
|
|
|
|
"health": "ONLINE",
|
|
|
|
"leaked": "0",
|
|
|
|
"listsnapshots": "off",
|
|
|
|
"name": "rpool",
|
|
|
|
"readonly": "off",
|
|
|
|
"size": "49.8G",
|
|
|
|
"version": "-"
|
|
|
|
}
|
|
|
|
name:
|
|
|
|
description: ZFS pool name
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: rpool
|
|
|
|
parsable:
|
|
|
|
description: if parsable output should be provided in machine friendly format.
|
|
|
|
returned: if 'parsable' is set to True
|
|
|
|
type: bool
|
2022-09-06 21:07:46 +02:00
|
|
|
sample: true
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
from ansible.module_utils.six import iteritems
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
2021-04-05 09:22:06 +02:00
|
|
|
cmd = [self.module.get_bin_path('zpool'), 'list', self.name]
|
2021-09-06 19:23:33 +02:00
|
|
|
rc, dummy, dummy = self.module.run_command(cmd)
|
|
|
|
return rc == 0
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def get_facts(self):
|
2021-04-05 09:22:06 +02:00
|
|
|
cmd = [self.module.get_bin_path('zpool'), 'get', '-H']
|
2020-03-09 10:11:07 +01:00
|
|
|
if self.parsable:
|
|
|
|
cmd.append('-p')
|
|
|
|
cmd.append('-o')
|
|
|
|
cmd.append('name,property,value')
|
|
|
|
cmd.append(self.properties)
|
|
|
|
if self.name:
|
|
|
|
cmd.append(self.name)
|
|
|
|
|
2021-09-06 19:23:33 +02:00
|
|
|
rc, out, err = self.module.run_command(cmd, check_rc=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-06 19:23:33 +02:00
|
|
|
for line in out.splitlines():
|
|
|
|
pool, prop, value = line.split('\t')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-06 19:23:33 +02:00
|
|
|
self._pools[pool].update({prop: value})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-06 19:23:33 +02:00
|
|
|
for k, v in iteritems(self._pools):
|
|
|
|
v.update({'name': k})
|
|
|
|
self.facts.append(v)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-09-06 19:23:33 +02:00
|
|
|
return {'ansible_zfs_pools': self.facts}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2021-09-06 19:23:33 +02:00
|
|
|
name=dict(aliases=['pool', 'zpool'], type='str'),
|
|
|
|
parsable=dict(default=False, type='bool'),
|
|
|
|
properties=dict(default='all', type='str'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
zpool_facts = ZPoolFacts(module)
|
|
|
|
|
2021-09-06 19:23:33 +02:00
|
|
|
result = {
|
|
|
|
'changed': False,
|
|
|
|
'name': zpool_facts.name,
|
|
|
|
}
|
2020-03-09 10:11:07 +01:00
|
|
|
if zpool_facts.parsable:
|
|
|
|
result['parsable'] = zpool_facts.parsable
|
|
|
|
|
|
|
|
if zpool_facts.name is not None:
|
|
|
|
if zpool_facts.pool_exists():
|
|
|
|
result['ansible_facts'] = zpool_facts.get_facts()
|
|
|
|
else:
|
|
|
|
module.fail_json(msg='ZFS pool %s does not exist!' % zpool_facts.name)
|
|
|
|
else:
|
|
|
|
result['ansible_facts'] = zpool_facts.get_facts()
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|