Repository: ansible/ansible

Issue:
osx_defaults "Invalid value. Expected value to be an array"
_From @d3v1an7 on July 21, 2016 8:38_

##### ISSUE TYPE
- Bug Report [or]
- Documentation Report
##### COMPONENT NAME

osx_defaults
##### ANSIBLE VERSION

```
ansible 2.2.0 (devel 372018dfce) last updated 2016/07/21 18:27:38 (GMT +1100)
  lib/ansible/modules/core: (detached HEAD 7de287237f) last updated 2016/07/21 18:27:59 (GMT +1100)
  lib/ansible/modules/extras: (detached HEAD 68ca157f3b) last updated 2016/07/21 18:28:05 (GMT +1100)
  config file =
  configured module search path = Default w/o overrides
```

Also had issue on 2.1.
##### CONFIGURATION

Playbook level ansible.cfg

```
retry_files_enabled = False
```
##### OS / ENVIRONMENT

10.11.5
##### SUMMARY

When attempting to interface with an array, I get the error: `Invalid value. Expected value to be an array`.
##### STEPS TO REPRODUCE

Copy code from docs: http://docs.ansible.com/ansible/osx_defaults_module.html

```
- osx_defaults:
    key: AppleLanguages
    type: array
    value: ["en", "nl"]
```
##### EXPECTED RESULTS

A green ok :)
##### ACTUAL RESULTS

```
TASK [system/install : osx_defaults] *******************************************
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Invalid value. Expected value to be an array"}
```


_Copied from original issue: ansible/ansible-modules-extras#2610_


Discussion:
_From @alikins on July 25, 2016 20:31_

Looking at that code, I'm not sure the example references from the docs could ever work.

(I apologize for all the weird 'value of the type value string' nonsense below, but it's kind of a weird module in that regard. I doubt this will make sense to me in a day either...)

The ansible arg spec type of the modules 'value' argument is None. So ansible won't ever attempt to convert the python value of the module args 'value' so it will always be either None or a str. But more importantly, it will never be a python list. The module code (the OSXDefaults class in particular) doesn't attempt to create a python list out of the string value of 'value' regardless of the module args value of it's 'type' arg. Even if the module is invoked with module args 'type=array' and 'value=["a","b"]', inside the module, the type of 'value' is a string and the code expects the type of 'value' to be a list.

Needed info: Did this ever work?  I suspect some ~2.1 era module arg changes could have changed it's behavior, but not in anyway that I would expect it have worked. 

Attempting to see if I can get this working in a playbook...

Hi @alikins, confirming this did work with a previous version of Ansible. I believe I was enforcing Ansible 2.0 when working on this last, as that was the minimum version required for the `osx_defaults` module.

Not sure if helpful, but here is an actual example of how I was using the module:

**Vars**

```
defaults:
  # Menu bar
  - domain: "com.apple.systemuiserver"
    settings:
      # Remove apps
      - { key: "menuExtras", type: "array", value: "[]" }
      # Add apps
      - { key: "menuExtras", type: "array", array_add: "true", value: "['/System/Library/CoreServices/Menu Extras/Battery.menu']" }
      - { key: "menuExtras", type: "array", array_add: "true", value: "['/System/Library/CoreServices/Menu Extras/Bluetooth.menu']" }
      - { key: "menuExtras", type: "array", array_add: "true", value: "['/System/Library/CoreServices/Menu Extras/AirPort.menu']" }
      - { key: "menuExtras", type: "array", array_add: "true", value: "['/System/Library/CoreServices/Menu Extras/Volume.menu']" }
```

**Role**

```
---
- name: write OSX defaults (user)
  osx_defaults:
    domain: "{{ item.0.domain }}"
    key: "{{ item.1.key }}"
    type: "{{ item.1.type }}"
    array_add: "{{ item.1.array_add | default(false) }}"
    value: "{{ item.1.value }}"
  with_subelements:
    - defaults
    - settings
```

_From @ansibot on August 17, 2016 22:59_

@ansible, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

_From @ansibot on September 11, 2016 14:53_

@ansible, ping. This issue is still waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_renotify_maintainer --->

_From @JPry on October 30, 2016 23:43_

I'm able to duplicate this issue with Ansible 2.1. 

_From @ansibot on December 6, 2016 22:6_

This repository has been locked. All new issues and pullrequests should be filed in https://github.com/ansible/ansible

Please read through the <a href="http://docs.ansible.com/ansible/dev_guide/repomerge.html#move-issues-and-prs-to-new-repo">repomerge</a> page in the dev guide. The guide contains links to tools which automatically move your issue or pullrequest to the ansible/ansible repo.

<!-- boilerplate: repomerge -->
Just checking back in. Still an issue on 2.3.0/devel, with either of the following:

``` yaml
- osx_defaults:
    key: AppleLanguages
    type: array
    value:
      - en
      - nl
```

``` yaml
- osx_defaults:
    key: AppleLanguages
    type: array
    value: ["en", "nl"]
```

Resolving patch:
diff --git a/lib/ansible/modules/system/osx_defaults.py b/lib/ansible/modules/system/osx_defaults.py
--- a/lib/ansible/modules/system/osx_defaults.py
+++ b/lib/ansible/modules/system/osx_defaults.py
@@ -116,7 +116,9 @@
 '''
 
 import datetime
-from ansible.module_utils.basic import *
+import re
+
+from ansible.module_utils.basic import AnsibleModule
 from ansible.module_utils.pycompat24 import get_exception
 
 # exceptions --------------------------------------------------------------- {{{
@@ -380,6 +382,7 @@ def main():
             value=dict(
                 default=None,
                 required=False,
+                type='raw'
             ),
             state=dict(
                 default="present",


Test patch: