| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2016, Adam Števko <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: zpool_facts |
| 11 |
short_description: Gather facts about ZFS pools |
| 12 |
description: |
| 13 |
- Gather facts from ZFS pool properties. |
| 14 |
author: Adam Števko (@xen0l) |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
- community.general._attributes.facts |
| 18 |
- community.general._attributes.facts_module |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- ZFS pool name. |
| 23 |
type: str |
| 24 |
aliases: ["pool", "zpool"] |
| 25 |
parsable: |
| 26 |
description: |
| 27 |
- Specifies if property values should be displayed in machine friendly format. |
| 28 |
type: bool |
| 29 |
default: false |
| 30 |
properties: |
| 31 |
description: |
| 32 |
- Specifies which dataset properties should be queried in comma-separated format. For more information about dataset |
| 33 |
properties, check zpool(1M) man page. |
| 34 |
type: str |
| 35 |
default: all |
| 36 |
""" |
| 37 |
|
| 38 |
EXAMPLES = r""" |
| 39 |
- name: Gather facts about ZFS pool rpool |
| 40 |
community.general.zpool_facts: pool=rpool |
| 41 |
|
| 42 |
- name: Gather space usage about all imported ZFS pools |
| 43 |
community.general.zpool_facts: properties='free,size' |
| 44 |
|
| 45 |
- name: Print gathered information |
| 46 |
ansible.builtin.debug: |
| 47 |
msg: 'ZFS pool {{ item.name }} has {{ item.free }} free space out of {{ item.size }}.' |
| 48 |
with_items: '{{ ansible_zfs_pools }}' |
| 49 |
""" |
| 50 |
|
| 51 |
RETURN = r""" |
| 52 |
ansible_facts: |
| 53 |
description: Dictionary containing all the detailed information about the ZFS pool facts. |
| 54 |
returned: success |
| 55 |
type: dict |
| 56 |
contains: |
| 57 |
ansible_zfs_pools: |
| 58 |
description: ZFS pool facts. |
| 59 |
returned: success |
| 60 |
type: list |
| 61 |
elements: dict |
| 62 |
sample: |
| 63 |
- "allocated": "3.46G" |
| 64 |
"altroot": "-" |
| 65 |
"autoexpand": "off" |
| 66 |
"autoreplace": "off" |
| 67 |
"bootfs": "rpool/ROOT/openindiana" |
| 68 |
"cachefile": "-" |
| 69 |
"capacity": "6%" |
| 70 |
"comment": "-" |
| 71 |
"dedupditto": "0" |
| 72 |
"dedupratio": "1.00x" |
| 73 |
"delegation": "on" |
| 74 |
"expandsize": "-" |
| 75 |
"failmode": "wait" |
| 76 |
"feature@async_destroy": "enabled" |
| 77 |
"feature@bookmarks": "enabled" |
| 78 |
"feature@edonr": "enabled" |
| 79 |
"feature@embedded_data": "active" |
| 80 |
"feature@empty_bpobj": "active" |
| 81 |
"feature@enabled_txg": "active" |
| 82 |
"feature@extensible_dataset": "enabled" |
| 83 |
"feature@filesystem_limits": "enabled" |
| 84 |
"feature@hole_birth": "active" |
| 85 |
"feature@large_blocks": "enabled" |
| 86 |
"feature@lz4_compress": "active" |
| 87 |
"feature@multi_vdev_crash_dump": "enabled" |
| 88 |
"feature@sha512": "enabled" |
| 89 |
"feature@skein": "enabled" |
| 90 |
"feature@spacemap_histogram": "active" |
| 91 |
"fragmentation": "3%" |
| 92 |
"free": "46.3G" |
| 93 |
"freeing": "0" |
| 94 |
"guid": "15729052870819522408" |
| 95 |
"health": "ONLINE" |
| 96 |
"leaked": "0" |
| 97 |
"listsnapshots": "off" |
| 98 |
"name": "rpool" |
| 99 |
"readonly": "off" |
| 100 |
"size": "49.8G" |
| 101 |
"version": "-" |
| 102 |
name: |
| 103 |
description: ZFS pool name. |
| 104 |
returned: success |
| 105 |
type: str |
| 106 |
sample: rpool |
| 107 |
parsable: |
| 108 |
description: If parsable output should be provided in machine friendly format. |
| 109 |
returned: if O(parsable=true) |
| 110 |
type: bool |
| 111 |
sample: true |
| 112 |
""" |
| 113 |
|
| 114 |
from collections import defaultdict |
| 115 |
|
| 116 |
from ansible.module_utils.basic import AnsibleModule |
| 117 |
|
| 118 |
|
| 119 |
class ZPoolFacts: |
| 120 |
def __init__(self, module): |
| 121 |
self.module = module |
| 122 |
self.name = module.params["name"] |
| 123 |
self.parsable = module.params["parsable"] |
| 124 |
self.properties = module.params["properties"] |
| 125 |
self._pools = defaultdict(dict) |
| 126 |
self.facts = [] |
| 127 |
|
| 128 |
def pool_exists(self): |
| 129 |
cmd = [self.module.get_bin_path("zpool"), "list", self.name] |
| 130 |
rc, dummy, dummy = self.module.run_command(cmd) |
| 131 |
return rc == 0 |
| 132 |
|
| 133 |
def get_facts(self): |
| 134 |
cmd = [self.module.get_bin_path("zpool"), "get", "-H"] |
| 135 |
if self.parsable: |
| 136 |
cmd.append("-p") |
| 137 |
cmd.append("-o") |
| 138 |
cmd.append("name,property,value") |
| 139 |
cmd.append(self.properties) |
| 140 |
if self.name: |
| 141 |
cmd.append(self.name) |
| 142 |
|
| 143 |
rc, out, err = self.module.run_command(cmd, check_rc=True) |
| 144 |
|
| 145 |
for line in out.splitlines(): |
| 146 |
pool, prop, value = line.split("\t") |
| 147 |
|
| 148 |
self._pools[pool].update({prop: value}) |
| 149 |
|
| 150 |
for k, v in self._pools.items(): |
| 151 |
v.update({"name": k}) |
| 152 |
self.facts.append(v) |
| 153 |
|
| 154 |
return {"ansible_zfs_pools": self.facts} |
| 155 |
|
| 156 |
|
| 157 |
def main(): |
| 158 |
module = AnsibleModule( |
| 159 |
argument_spec=dict( |
| 160 |
name=dict(aliases=["pool", "zpool"], type="str"), |
| 161 |
parsable=dict(default=False, type="bool"), |
| 162 |
properties=dict(default="all", type="str"), |
| 163 |
), |
| 164 |
supports_check_mode=True, |
| 165 |
) |
| 166 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 167 |
|
| 168 |
zpool_facts = ZPoolFacts(module) |
| 169 |
|
| 170 |
result = { |
| 171 |
"changed": False, |
| 172 |
"name": zpool_facts.name, |
| 173 |
} |
| 174 |
if zpool_facts.parsable: |
| 175 |
result["parsable"] = zpool_facts.parsable |
| 176 |
|
| 177 |
if zpool_facts.name is not None: |
| 178 |
if zpool_facts.pool_exists(): |
| 179 |
result["ansible_facts"] = zpool_facts.get_facts() |
| 180 |
else: |
| 181 |
module.fail_json(msg=f"ZFS pool {zpool_facts.name} does not exist!") |
| 182 |
else: |
| 183 |
result["ansible_facts"] = zpool_facts.get_facts() |
| 184 |
|
| 185 |
module.exit_json(**result) |
| 186 |
|
| 187 |
|
| 188 |
if __name__ == "__main__": |
| 189 |
main() |