Passing complex data (JSON) to a module

How can I obtain a complex data structure within an Ansible module?

Given the following vars_file:

have you tried?:

  zlist = dict(required=True, type=dict)

default type is a string, which you would have to json.loads to create
a complex structure.

have you tried?:

  zlist = dict(required=True, type=dict)

I tried 'dict' in single quotes with no noticeable difference.

   zlist = dict(required=True, type='dict')

default type is a string, which you would have to json.loads to create
a complex structure.

That's what I thought, but json.loads(zlist) reported either a parse
error. I'm 500 Km distant from the machine unfortunately, but I did
notice that the quoting of the string was incorrect for JSON. Instead
of the double quotes in, say,

        { "name" : "Jane" }

I saw single quotes (in the zlist string)

        { 'name' : 'Jane' }

Something's not quite right, but I can't figure it out.

        -JP

have you tried?:

  zlist = dict(required=True, type=dict)

I tried 'dict' in single quotes with no noticeable difference.

   zlist = dict(required=True, type='dict')

Looking at the zones variable, it should probably be type='list' but
this isn't actually the root of the problem.

         tasks:
         - name: COMplex DATa
           action: comdata zlist="{{ zones }}"
           register: n

The problem is that the key value parsing is reading zones as a string
instead of as a list. In my testing either of these alternatives will
work:

- name: COMplex DATa
  comdata:
    zlist: "{{ zones }}"
  register: n

- name: COMplex DATa
  action: comdata
  args:
    zlist: "{{ zones }}"
  register: n

jimi-c, bcoca: Is this a bug in the key-value parsing or simply a way
that it is different from the better complex-args syntax?

-Toshio

In my testing either of these alternatives will work:

Thank you very much, Toshio!

        -JP

The problem is that the key value parsing is reading zones as a string
instead of as a list. In my testing either of these alternatives will
work:

- name: COMplex DATa
  comdata:
    zlist: "{{ zones }}"
  register: n

Followup after testing: yes, that works, but what the module gets isn't
JSON. I've tried with type=str, list and dict. The best is `str' but
json.loads() won't touch it because the elements are enclosed in single
quotes instead of in double quotes. I can, using ast.literal_eval(),
parse it into a Python data structure, but it's certainly not JSON.

The module and its data and simple playbook are at [1].

Regards,

        -JP

[1] https://gist.github.com/jpmens/4b67010fc6cbe9ff42bc

The data type of ‘zones’ appears to actually be a list. Switching the type to ‘list’ appears to resolve your issue.

Then you can remove your json/ast block and change ‘data’ out for ‘zlist’ and I get:

PLAY [localhost] **************************************************************

TASK: [COMplex DATa] **********************************************************
changed: [localhost] => {“changed”: true, “msg”: “OK”, “newdata”: [“ONE.DE”, “THIRTEEN.COM”]}

PLAY RECAP ********************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0

The data type of 'zones' appears to actually be a list. Switching the type
to 'list' appears to resolve your issue.

With 2.0 yes. I swear this didn't work on 1.8.something ... (Can't test
now though.)

Thank you.

        -JP