From 1110e93c5d252840d44dfee35a2d1dffa0391f2b Mon Sep 17 00:00:00 2001 From: Kazufumi NOTO Date: Wed, 9 Dec 2020 14:29:58 +0900 Subject: [PATCH] Bugfix: Fix parsing array values from osx_defaults (#358) * Bugfix: Fix parsing array values in osx_defaults Unquote values and unescape double quotes when reading array values from defaults. * Fix fragments: fix_parsing_array_values_in_osx_defaults Co-authored-by: Felix Fontein * add test code for Bugfix: Fix parsing array values from osx_defaults * handle spaces after the comma Co-authored-by: Felix Fontein --- ...x_parsing_array_values_in_osx_defaults.yml | 2 + plugins/modules/system/osx_defaults.py | 4 +- .../targets/osx_defaults/tasks/main.yml | 43 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/fix_parsing_array_values_in_osx_defaults.yml diff --git a/changelogs/fragments/fix_parsing_array_values_in_osx_defaults.yml b/changelogs/fragments/fix_parsing_array_values_in_osx_defaults.yml new file mode 100644 index 0000000000..aa6788df85 --- /dev/null +++ b/changelogs/fragments/fix_parsing_array_values_in_osx_defaults.yml @@ -0,0 +1,2 @@ +bugfixes: + - osx_defaults - unquote values and unescape double quotes when reading array values (https://github.com/ansible-collections/community.general/pull/358). diff --git a/plugins/modules/system/osx_defaults.py b/plugins/modules/system/osx_defaults.py index 06c41e9ec5..a036290879 100644 --- a/plugins/modules/system/osx_defaults.py +++ b/plugins/modules/system/osx_defaults.py @@ -237,8 +237,8 @@ class OSXDefaults(object): value.pop(0) value.pop(-1) - # Remove extra spaces and comma (,) at the end of values - value = [re.sub(',$', '', x.strip(' ')) for x in value] + # Remove spaces at beginning and comma (,) at the end, unquote and unescape double quotes + value = [re.sub('^ *"?|"?,? *$', '', x.replace('\\"', '"')) for x in value] return value diff --git a/tests/integration/targets/osx_defaults/tasks/main.yml b/tests/integration/targets/osx_defaults/tasks/main.yml index 497a5aa150..af4667ce28 100644 --- a/tests/integration/targets/osx_defaults/tasks/main.yml +++ b/tests/integration/targets/osx_defaults/tasks/main.yml @@ -208,3 +208,46 @@ - assert: that: "{{ item.changed }}" with_items: "{{ test_data_types.results }}" + + +- name: Ensure test key does not exist + osx_defaults: + domain: com.ansible.fake_array_value + key: ExampleArrayKey + state: absent + +- name: add array value for the first time + osx_defaults: + domain: com.ansible.fake_array_value + key: ExampleArrayKey + value: + - 'Value with spaces' + type: array + array_add: yes + register: test_array_add + +- assert: + that: test_array_add.changed + +- name: add for the second time, should be skipped + osx_defaults: + domain: com.ansible.fake_array_value + key: ExampleArrayKey + value: + - 'Value with spaces' + type: array + array_add: yes + register: test_array_add + +- assert: + that: not test_array_add.changed + +- name: Clean up test key + osx_defaults: + domain: com.ansible.fake_array_value + key: ExampleArrayKey + state: absent + register: test_array_add + +- assert: + that: test_array_add.changed