From ce5aea790d5fc8c3664a323a3e6551da065619fc Mon Sep 17 00:00:00 2001
From: sam-lunt <samuel.j.lunt@gmail.com>
Date: Thu, 4 Mar 2021 00:47:45 -0600
Subject: [PATCH] zfs: avoid errors with creation-only properties (#1833)

* avoid errors with creation-only properties

* add changelog fragment

* Apply suggestion to changelog fragment

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Felix Fontein <felix@fontein.de>
---
 changelogs/fragments/1833-zfs-creation-only-properties.yaml | 2 ++
 plugins/modules/storage/zfs/zfs.py                          | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/fragments/1833-zfs-creation-only-properties.yaml

diff --git a/changelogs/fragments/1833-zfs-creation-only-properties.yaml b/changelogs/fragments/1833-zfs-creation-only-properties.yaml
new file mode 100644
index 0000000000..deb972a6d2
--- /dev/null
+++ b/changelogs/fragments/1833-zfs-creation-only-properties.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+  - zfs - some ZFS properties could be passed when the dataset/volume did not exist, but would fail if the dataset already existed, even if the property matched what was specified in the ansible task (https://github.com/ansible-collections/community.general/issues/868, https://github.com/ansible-collections/community.general/pull/1833).
diff --git a/plugins/modules/storage/zfs/zfs.py b/plugins/modules/storage/zfs/zfs.py
index 8013dd1128..fe693a5045 100644
--- a/plugins/modules/storage/zfs/zfs.py
+++ b/plugins/modules/storage/zfs/zfs.py
@@ -203,7 +203,10 @@ class Zfs(object):
         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: