From cec99472d92144de2940e4c424463d736dde4358 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 9 Apr 2024 07:41:48 +0200 Subject: [PATCH] [PR #8173/bc2ff24f backport][stable-8] Add check_type option, to allow defaults type changes (#8203) Add check_type option, to allow defaults type changes (#8173) * Add check_type option, to allow defaults type changes * Add changelog fragment * Changelog fragments are yaml, not markdown * Update changelogs/fragments/8173-osx_defaults-check_type.yml Co-authored-by: Felix Fontein * Update plugins/modules/osx_defaults.py Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit bc2ff24f74aa4f9120f2f6a8d2d256fdfcd83871) Co-authored-by: Kris Matthews --- .../fragments/8173-osx_defaults-check_type.yml | 2 ++ plugins/modules/osx_defaults.py | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/8173-osx_defaults-check_type.yml diff --git a/changelogs/fragments/8173-osx_defaults-check_type.yml b/changelogs/fragments/8173-osx_defaults-check_type.yml new file mode 100644 index 0000000000..a35f609bf3 --- /dev/null +++ b/changelogs/fragments/8173-osx_defaults-check_type.yml @@ -0,0 +1,2 @@ +minor_changes: + - osx_defaults - add option ``check_types`` to enable changing the type of existing defaults on the fly (https://github.com/ansible-collections/community.general/pull/8173). diff --git a/plugins/modules/osx_defaults.py b/plugins/modules/osx_defaults.py index 336e953320..db5d889a37 100644 --- a/plugins/modules/osx_defaults.py +++ b/plugins/modules/osx_defaults.py @@ -50,6 +50,13 @@ options: type: str choices: [ array, bool, boolean, date, float, int, integer, string ] default: string + check_type: + description: + - Checks if the type of the provided O(value) matches the type of an existing default. + - If the types do not match, raises an error. + type: bool + default: true + version_added: 8.6.0 array_add: description: - Add new elements to the array for a key which has an array as its value. @@ -158,6 +165,7 @@ class OSXDefaults(object): self.domain = module.params['domain'] self.host = module.params['host'] self.key = module.params['key'] + self.check_type = module.params['check_type'] self.type = module.params['type'] self.array_add = module.params['array_add'] self.value = module.params['value'] @@ -349,10 +357,11 @@ class OSXDefaults(object): self.delete() return True - # There is a type mismatch! Given type does not match the type in defaults - value_type = type(self.value) - if self.current_value is not None and not isinstance(self.current_value, value_type): - raise OSXDefaultsException("Type mismatch. Type in defaults: %s" % type(self.current_value).__name__) + # Check if there is a type mismatch, e.g. given type does not match the type in defaults + if self.check_type: + value_type = type(self.value) + if self.current_value is not None and not isinstance(self.current_value, value_type): + raise OSXDefaultsException("Type mismatch. Type in defaults: %s" % type(self.current_value).__name__) # Current value matches the given value. Nothing need to be done. Arrays need extra care if self.type == "array" and self.current_value is not None and not self.array_add and \ @@ -383,6 +392,7 @@ def main(): domain=dict(type='str', default='NSGlobalDomain'), host=dict(type='str'), key=dict(type='str', no_log=False), + check_type=dict(type='bool', default=True), type=dict(type='str', default='string', choices=['array', 'bool', 'boolean', 'date', 'float', 'int', 'integer', 'string']), array_add=dict(type='bool', default=False), value=dict(type='raw'),