| 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: zfs_facts |
| 11 |
short_description: Gather facts about ZFS datasets |
| 12 |
description: |
| 13 |
- Gather facts from ZFS dataset 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 dataset name. |
| 23 |
required: true |
| 24 |
aliases: ["ds", "dataset"] |
| 25 |
type: str |
| 26 |
recurse: |
| 27 |
description: |
| 28 |
- Specifies if properties for any children should be recursively displayed. |
| 29 |
type: bool |
| 30 |
default: false |
| 31 |
parsable: |
| 32 |
description: |
| 33 |
- Specifies if property values should be displayed in machine friendly format. |
| 34 |
type: bool |
| 35 |
default: false |
| 36 |
properties: |
| 37 |
description: |
| 38 |
- Specifies which dataset properties should be queried in comma-separated format. For more information about dataset |
| 39 |
properties, check zfs(1M) man page. |
| 40 |
default: all |
| 41 |
type: str |
| 42 |
type: |
| 43 |
description: |
| 44 |
- Specifies which datasets types to display. Multiple values have to be provided as a list or in comma-separated form. |
| 45 |
- Value V(all) cannot be used together with other values. |
| 46 |
choices: ['all', 'filesystem', 'volume', 'snapshot', 'bookmark'] |
| 47 |
default: [all] |
| 48 |
type: list |
| 49 |
elements: str |
| 50 |
depth: |
| 51 |
description: |
| 52 |
- Specifies recursion depth. |
| 53 |
type: int |
| 54 |
default: 0 |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = r""" |
| 58 |
- name: Gather facts about ZFS dataset rpool/export/home |
| 59 |
community.general.zfs_facts: |
| 60 |
dataset: rpool/export/home |
| 61 |
|
| 62 |
- name: Report space usage on ZFS filesystems under data/home |
| 63 |
community.general.zfs_facts: |
| 64 |
name: data/home |
| 65 |
recurse: true |
| 66 |
type: filesystem |
| 67 |
|
| 68 |
- ansible.builtin.debug: |
| 69 |
msg: 'ZFS dataset {{ item.name }} consumes {{ item.used }} of disk space.' |
| 70 |
with_items: '{{ ansible_zfs_datasets }}' |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = r""" |
| 74 |
name: |
| 75 |
description: ZFS dataset name. |
| 76 |
returned: success |
| 77 |
type: str |
| 78 |
sample: rpool/var/spool |
| 79 |
parsable: |
| 80 |
description: If parsable output should be provided in machine friendly format. |
| 81 |
returned: if O(parsable=True) |
| 82 |
type: bool |
| 83 |
sample: true |
| 84 |
recurse: |
| 85 |
description: If we should recurse over ZFS dataset. |
| 86 |
returned: if O(recurse=True) |
| 87 |
type: bool |
| 88 |
sample: true |
| 89 |
ansible_facts: |
| 90 |
description: Dictionary containing all the detailed information about the ZFS facts. |
| 91 |
returned: success |
| 92 |
type: dict |
| 93 |
contains: |
| 94 |
zfs_datasets: |
| 95 |
description: ZFS dataset facts. |
| 96 |
returned: always |
| 97 |
type: list |
| 98 |
elements: dict |
| 99 |
sample: |
| 100 |
- "aclinherit": "restricted" |
| 101 |
"aclmode": "discard" |
| 102 |
"atime": "on" |
| 103 |
"available": "43.8G" |
| 104 |
"canmount": "on" |
| 105 |
"casesensitivity": "sensitive" |
| 106 |
"checksum": "on" |
| 107 |
"compression": "off" |
| 108 |
"compressratio": "1.00x" |
| 109 |
"copies": "1" |
| 110 |
"creation": "Thu Jun 16 11:37 2016" |
| 111 |
"dedup": "off" |
| 112 |
"devices": "on" |
| 113 |
"exec": "on" |
| 114 |
"filesystem_count": "none" |
| 115 |
"filesystem_limit": "none" |
| 116 |
"logbias": "latency" |
| 117 |
"logicalreferenced": "18.5K" |
| 118 |
"logicalused": "3.45G" |
| 119 |
"mlslabel": "none" |
| 120 |
"mounted": "yes" |
| 121 |
"mountpoint": "/rpool" |
| 122 |
"name": "rpool" |
| 123 |
"nbmand": "off" |
| 124 |
"normalization": "none" |
| 125 |
"org.openindiana.caiman:install": "ready" |
| 126 |
"primarycache": "all" |
| 127 |
"quota": "none" |
| 128 |
"readonly": "off" |
| 129 |
"recordsize": "128K" |
| 130 |
"redundant_metadata": "all" |
| 131 |
"refcompressratio": "1.00x" |
| 132 |
"referenced": "29.5K" |
| 133 |
"refquota": "none" |
| 134 |
"refreservation": "none" |
| 135 |
"reservation": "none" |
| 136 |
"secondarycache": "all" |
| 137 |
"setuid": "on" |
| 138 |
"sharenfs": "off" |
| 139 |
"sharesmb": "off" |
| 140 |
"snapdir": "hidden" |
| 141 |
"snapshot_count": "none" |
| 142 |
"snapshot_limit": "none" |
| 143 |
"sync": "standard" |
| 144 |
"type": "filesystem" |
| 145 |
"used": "4.41G" |
| 146 |
"usedbychildren": "4.41G" |
| 147 |
"usedbydataset": "29.5K" |
| 148 |
"usedbyrefreservation": "0" |
| 149 |
"usedbysnapshots": "0" |
| 150 |
"utf8only": "off" |
| 151 |
"version": "5" |
| 152 |
"vscan": "off" |
| 153 |
"written": "29.5K" |
| 154 |
"xattr": "on" |
| 155 |
"zoned": "off" |
| 156 |
""" |
| 157 |
|
| 158 |
from collections import defaultdict |
| 159 |
|
| 160 |
from ansible.module_utils.basic import AnsibleModule |
| 161 |
|
| 162 |
SUPPORTED_TYPES = ["all", "filesystem", "volume", "snapshot", "bookmark"] |
| 163 |
|
| 164 |
|
| 165 |
class ZFSFacts: |
| 166 |
def __init__(self, module): |
| 167 |
self.module = module |
| 168 |
|
| 169 |
self.name = module.params["name"] |
| 170 |
self.recurse = module.params["recurse"] |
| 171 |
self.parsable = module.params["parsable"] |
| 172 |
self.properties = module.params["properties"] |
| 173 |
self.type = module.params["type"] |
| 174 |
self.depth = module.params["depth"] |
| 175 |
|
| 176 |
self._datasets = defaultdict(dict) |
| 177 |
self.facts = [] |
| 178 |
|
| 179 |
def dataset_exists(self): |
| 180 |
cmd = [self.module.get_bin_path("zfs"), "list", self.name] |
| 181 |
|
| 182 |
(rc, out, err) = self.module.run_command(cmd) |
| 183 |
|
| 184 |
return rc == 0 |
| 185 |
|
| 186 |
def get_facts(self): |
| 187 |
cmd = [self.module.get_bin_path("zfs"), "get", "-H"] |
| 188 |
if self.parsable: |
| 189 |
cmd.append("-p") |
| 190 |
if self.recurse: |
| 191 |
cmd.append("-r") |
| 192 |
if self.depth != 0: |
| 193 |
cmd.append("-d") |
| 194 |
cmd.append(f"{self.depth}") |
| 195 |
if self.type: |
| 196 |
cmd.append("-t") |
| 197 |
cmd.append(",".join(self.type)) |
| 198 |
cmd.extend(["-o", "name,property,value", self.properties, self.name]) |
| 199 |
|
| 200 |
(rc, out, err) = self.module.run_command(cmd, check_rc=True) |
| 201 |
|
| 202 |
for line in out.splitlines(): |
| 203 |
dataset, property, value = line.split("\t") |
| 204 |
|
| 205 |
self._datasets[dataset].update({property: value}) |
| 206 |
|
| 207 |
for k, v in self._datasets.items(): |
| 208 |
v.update({"name": k}) |
| 209 |
self.facts.append(v) |
| 210 |
|
| 211 |
return {"ansible_zfs_datasets": self.facts} |
| 212 |
|
| 213 |
|
| 214 |
def main(): |
| 215 |
module = AnsibleModule( |
| 216 |
argument_spec=dict( |
| 217 |
name=dict(required=True, aliases=["ds", "dataset"], type="str"), |
| 218 |
recurse=dict(default=False, type="bool"), |
| 219 |
parsable=dict(default=False, type="bool"), |
| 220 |
properties=dict(default="all", type="str"), |
| 221 |
type=dict(default="all", type="list", elements="str", choices=SUPPORTED_TYPES), |
| 222 |
depth=dict(default=0, type="int"), |
| 223 |
), |
| 224 |
supports_check_mode=True, |
| 225 |
) |
| 226 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 227 |
|
| 228 |
if "all" in module.params["type"] and len(module.params["type"]) > 1: |
| 229 |
module.fail_json(msg="Value 'all' for parameter 'type' is mutually exclusive with other values") |
| 230 |
|
| 231 |
zfs_facts = ZFSFacts(module) |
| 232 |
|
| 233 |
result = {} |
| 234 |
result["changed"] = False |
| 235 |
result["name"] = zfs_facts.name |
| 236 |
|
| 237 |
if zfs_facts.parsable: |
| 238 |
result["parsable"] = zfs_facts.parsable |
| 239 |
|
| 240 |
if zfs_facts.recurse: |
| 241 |
result["recurse"] = zfs_facts.recurse |
| 242 |
|
| 243 |
if not zfs_facts.dataset_exists(): |
| 244 |
module.fail_json(msg=f"ZFS dataset {zfs_facts.name} does not exist!") |
| 245 |
|
| 246 |
result["ansible_facts"] = zfs_facts.get_facts() |
| 247 |
|
| 248 |
module.exit_json(**result) |
| 249 |
|
| 250 |
|
| 251 |
if __name__ == "__main__": |
| 252 |
main() |