Repository: ansible-collections/community.general

Issue:
ZFS tries to change immutable attributes
##### SUMMARY

Module `zfs` tries to set immutable attributes like `volblocksize`. Seems to be a regression, see [ansible/ansible-modules-extras#769](https://github.com/ansible/ansible-modules-extras/issues/769) and [ansible/ansible-modules-extras#1378](https://github.com/ansible/ansible-modules-extras/pull/1378)
##### ISSUE TYPE

    * Bug Report


##### COMPONENT NAME

zfs
##### ANSIBLE VERSION

```
ansible 2.8.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.13 (default, Sep 26 2018, 18:42:22) [GCC 6.3.0 20170516]
```

##### CONFIGURATION

```
ANSIBLE_PIPELINING(/etc/ansible/ansible.cfg) = True
DEFAULT_VAULT_PASSWORD_FILE(env: ANSIBLE_VAULT_PASSWORD_FILE) = /root/.ansible_vault_password.txt
INVENTORY_ENABLED(/etc/ansible/ansible.cfg) = [u'k8s', u'host_list', u'script', u'yaml', u'ini', u'constructed']
RETRY_FILES_ENABLED(/etc/ansible/ansible.cfg) = False
```

##### OS / ENVIRONMENT

Debian GNU/Linux 9.9 (Stretch)
##### STEPS TO REPRODUCE

```yaml
- name: "Create zvol"
  zfs:
    name: mypool/folder/myzvol
    state: present
    extra_zfs_properties:
      volsize: '10G'
      volblocksize: 128K
```

##### EXPECTED RESULTS

Module `zfs` should not try to set immutable attributes like `volblocksize` on existing fileystems/volumes.
##### ACTUAL RESULTS

```
fatal: [nodename]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "createparent": null,
            "extra_zfs_properties": {
                "volblocksize": "128K",
                "volsize": "10G"
            },
            "name": "mypool/folder/myzvol",
            "origin": null,
            "state": "present"
        }
    },
    "msg": "cannot set property for 'mypool/folder/myzvol: 'volblocksize' is readonly\n"
}
```


Similarly with `encryption`:


```yaml
            extra_zfs_properties:
              encryption: aes-256-gcm
              keyformat: passphrase
              keylocation: file:///tmp/zfs-pass
              mountpoint: legacy
```

```
fatal: [barbican]: FAILED! => {"changed": false, "msg": "cannot set property for 'barbican/nixos': 'encryption' is readonly\n"}
```

cf https://github.com/ansible/ansible/issues/58759 https://github.com/ansible/ansible/issues/29422


Discussion:
Files identified in the description:
* [`plugins/modules/system/beadm.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/system/beadm.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @MorrisA @bcoca @d-little @fishman @flynn1973 @gforster @jasperla @jpdasma @kairoaraujo @marvin-sinister @mator @molekuul @ramooncamacho @scathatheworm @troy2914 @wtcross @xen0l
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
!component =plugins/modules/storage/zfs/zfs.py
Files identified in the description:
* [`plugins/modules/storage/zfs/zfs.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/storage/zfs/zfs.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @johanwiren
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Some additional properties that can only be set at zfs dataset creation time:
* `casesensitivity`
* `normalization`
* `utf8only`
@sam-lunt created a PR to fix this (#1833), could you please check whether that PR fixes the problem for you, and/or review it?

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
@@ -203,7 +203,10 @@ def get_current_properties(self):
         rc, out, err = self.module.run_command(" ".join(cmd))
         properties = dict()
         for prop, value, source in [l.split('\t')[1:4] for l in out.splitlines()]:
-            if source == 'local':
+            # include source '-' so that creation-only properties are not removed
+            # to avoids errors when the dataset already exists and the property is not changed
+            # this scenario is most likely when the same playbook is run more than once
+            if source == 'local' or source == '-':
                 properties[prop] = value
         # Add alias for enhanced sharing properties
         if self.enhanced_sharing:


Test patch: