Repository: ansible-collections/community.general

Issue:
ZFS Snapshot fails on root zvol
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Explain the problem briefly below -->
When trying to create a ZFS snapshot of the root zvol, using: 
```yaml
zfs:
  name: "hobraid2@daily_{{ ansible_date_time.date }}"
  state: present
```
it fails with error `"cannot open 'hobraid2@daily_2020-09-18': invalid character '@' in pool name"`
##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
zfs
##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible 2.9.13
  config file = /mnt/hob-raid/docker/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.6 (default, Dec 19 2019, 22:52:49) [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
DEFAULT_HOST_LIST(/mnt/hob-raid/docker/ansible/ansible.cfg) = ['/mnt/hob-raid/docker/ansible/inventory.yml']
HOST_KEY_CHECKING(/mnt/hob-raid/docker/ansible/ansible.cfg) = False
```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->
Fedora 31

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
1 - Create A root zpool, in my case hobraid2
```
# zpool list
NAME       SIZE  ALLOC   FREE  CKPOINT  EXPANDSZ   FRAG    CAP  DEDUP    HEALTH  ALTROOT
hobraid2  3.62T  1.33T  2.30T        -         -     3%    36%  1.00x    ONLINE  -
```
2 - attempt to use the zfs module in ansible to create a snapshot, e.g:
```yaml
zfs:
  name: "hobraid2@daily_{{ ansible_date_time.date }}"
  state: present
```
<!--- Paste example playbooks or commands between quotes below -->
```yaml
zfs:
  name: "hobraid2@daily_{{ ansible_date_time.date }}"
  state: present
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
The snapshot is created without error

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
`"cannot open 'hobraid2@daily_2020-09-18': invalid character '@' in pool name"`
<!--- Paste verbatim command output between quotes -->
```paste below
fatal: [hob-ssv]: FAILED! => {
    "changed": false,
    "cmd": "/usr/sbin/zpool get version ''",
    "invocation": {
        "module_args": {
            "extra_zfs_properties": {},
            "name": "hobraid2@daily_2020-09-18",
            "origin": null,
            "state": "present"
        }
    },
    "msg": "cannot open 'hobraid2@daily_2020-09-18':********@' in pool name",
    "rc": 1,
    "stderr": "cannot open 'hobraid2@daily_2020-09-18': invalid character '@' in pool name\n",
    "stderr_lines": [
        "cannot open 'hobraid2@daily_2020-09-18': invalid character '@' in pool name"
    ],
    "stdout": "",
    "stdout_lines": []
}
```

It would appear that this line in the ZFS module gets the pool name by separating it out using split('/'), however when running commands on the root zvol, there is no /, causing the full name to be passed into zpool version generating the error.

https://github.com/ansible-collections/community.general/blob/51121e54d02e663fa26599cbebb667bec666ce37/plugins/modules/storage/zfs/zfs.py#L93


Discussion:

Resolving patch:
diff --git a/plugins/modules/storage/zfs/zfs.py b/plugins/modules/storage/zfs/zfs.py
--- a/plugins/modules/storage/zfs/zfs.py
+++ b/plugins/modules/storage/zfs/zfs.py
@@ -90,7 +90,7 @@ def __init__(self, module, name, properties):
         self.changed = False
         self.zfs_cmd = module.get_bin_path('zfs', True)
         self.zpool_cmd = module.get_bin_path('zpool', True)
-        self.pool = name.split('/')[0]
+        self.pool = name.split('/')[0].split('@')[0]
         self.is_solaris = os.uname()[0] == 'SunOS'
         self.is_openzfs = self.check_openzfs()
         self.enhanced_sharing = self.check_enhanced_sharing()


Test patch: