# Passing complex data (JSON) to a module

**URL:** <https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030>\
**Category:** Ansible Project\
**Created:** [April 21, 2015, 5:31am UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030 "2015-04-21T05:31:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan-Piet\_Mens](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jan-Piet\_Mens](https://forum.ansible.com/u/Jan-Piet_Mens)\
**Post date:** [April 21, 2015, 5:31am UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/1 "2015-04-21T05:31:18Z")

</div>

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

Given the following vars\_file:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [April 21, 2015, 1:28pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/2 "2015-04-21T13:28:30Z")

</div>

have you tried?:

&nbsp;&nbsp;zlist = dict(required=True, type=dict)

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

---

<div class="post-metadata">

**Author:** ![Jan-Piet\_Mens](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jan-Piet\_Mens](https://forum.ansible.com/u/Jan-Piet_Mens)\
**Post date:** [April 21, 2015, 9:42pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/3 "2015-04-21T21:42:34Z")

</div>

> have you tried?:
> 
> &nbsp;&nbsp;zlist = dict(required=True, type=dict)

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

&nbsp;&nbsp;&nbsp;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,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ "name" : "Jane" }

I saw single quotes (in the zlist string)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ 'name' : 'Jane' }

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [April 22, 2015, 12:57pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/4 "2015-04-22T12:57:43Z")

</div>

> > have you tried?:
> > 
> > &nbsp;&nbsp;zlist = dict(required=True, type=dict)
> 
> I tried 'dict' in single quotes with no noticeable difference.
> 
> &nbsp;&nbsp;&nbsp;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.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: COMplex DATa  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: comdata zlist="{{ zones }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;comdata:  
&nbsp;&nbsp;&nbsp;&nbsp;zlist: "{{ zones }}"  
&nbsp;&nbsp;register: n

- name: COMplex DATa  
&nbsp;&nbsp;action: comdata  
&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;zlist: "{{ zones }}"  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Jan-Piet\_Mens](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jan-Piet\_Mens](https://forum.ansible.com/u/Jan-Piet_Mens)\
**Post date:** [April 24, 2015, 8:23pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/5 "2015-04-24T20:23:42Z")

</div>

> In my testing either of these alternatives will work:

Thank you very much, Toshio!

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

---

<div class="post-metadata">

**Author:** ![Jan-Piet\_Mens](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jan-Piet\_Mens](https://forum.ansible.com/u/Jan-Piet_Mens)\
**Post date:** [April 27, 2015, 7:30pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/6 "2015-04-27T19:30:06Z")

</div>

> 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  
> &nbsp;&nbsp;comdata:  
> &nbsp;&nbsp;&nbsp;&nbsp;zlist: "{{ zones }}"  
> &nbsp;&nbsp;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,

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP

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

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [April 27, 2015, 7:42pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/7 "2015-04-27T19:42:36Z")

</div>

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](http://ONE.DE)”, “[THIRTEEN.COM](http://THIRTEEN.COM)”]}

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

---

<div class="post-metadata">

**Author:** ![Jan-Piet\_Mens](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Jan-Piet\_Mens](https://forum.ansible.com/u/Jan-Piet_Mens)\
**Post date:** [April 28, 2015, 7:26pm UTC](https://forum.ansible.com/t/passing-complex-data-json-to-a-module/18030/8 "2015-04-28T19:26:36Z")

</div>

> 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.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-JP
