osx_defaults: add dict support (#11659)

* osx_defaults: add dict support

* add changelog frag

* osx_defaults: fix dict idempotency by using plutil -extract for type-preserving read

The previous approach piped `defaults read` output (old-style plist text)
through `plutil -convert json`. Old-style plist loses boolean type info
(booleans appear as 1/0, indistinguishable from integers), causing the
comparison to fail and reporting changed=True on every run.

Fix by exporting the domain binary plist to a temp file and using
`plutil -extract key json` which correctly preserves all plist types
(booleans stay true/false, integers stay integers, etc.).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* change param from bool to str

* Apply suggestion from review

* Update plugins/modules/osx_defaults.py

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

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky
2026-03-24 08:16:21 +13:00
committed by GitHub
parent 7c039918e0
commit d6cb56c022
3 changed files with 187 additions and 2 deletions

View File

@@ -253,3 +253,97 @@
- assert:
that: test_array_add.changed
- name: Ensure dict test key does not exist
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKey
state: absent
- name: Write dict value for the first time
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKey
type: dict
value:
General: true
OpenWith: true
register: test_dict_write
- assert:
that: test_dict_write.changed
- name: Write same dict value again, should not change
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKey
type: dict
value:
General: true
OpenWith: true
register: test_dict_write
- assert:
that: not test_dict_write.changed
- name: Write different dict value, should change
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKey
type: dict
value:
General: true
OpenWith: false
register: test_dict_write
- assert:
that: test_dict_write.changed
- name: Add new key via dict_add
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKey
type: dict
dict_mode: add
value:
Privileges: true
register: test_dict_add
- assert:
that: test_dict_add.changed
- name: Add same key via dict_add again, should not change
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKey
type: dict
dict_mode: add
value:
Privileges: true
register: test_dict_add
- assert:
that: not test_dict_add.changed
- name: Use dict_add on non-existent key, should create it
osx_defaults:
domain: com.ansible.fake_dict_value
key: ExampleDictKeyNew
type: dict
dict_mode: add
value:
Alpha: true
register: test_dict_add_new
- assert:
that: test_dict_add_new.changed
- name: Clean up dict test keys
osx_defaults:
domain: com.ansible.fake_dict_value
key: "{{ item }}"
state: absent
loop:
- ExampleDictKey
- ExampleDictKeyNew