# module params doing JSON conversion?

**URL:** <https://forum.ansible.com/t/module-params-doing-json-conversion/15008>\
**Category:** Ansible Developer\
**Tags:** aws\
**Created:** [April 13, 2014, 4:30pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008 "2014-04-13T16:30:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [April 13, 2014, 4:30pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/1 "2014-04-13T16:30:23Z")

</div>

Hi all,

I’ve got a JSON file, web\_iam\_trust\_policy.json:

{  
&nbsp;&nbsp;&nbsp;&nbsp;"Version": "2012-10-17",  
&nbsp;&nbsp;&nbsp;&nbsp;"Statement":  
&nbsp;&nbsp;&nbsp;&nbsp;[  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Sid": "",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Effect": "Allow",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Principal":  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Service": "ec2.amazonaws.com"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Action": "sts:AssumeRole"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

I’m trying to use it in a lookup:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trust\_policy: "{{ lookup('file', 'files/aws/web\_iam\_trust\_policy.json') }}"

However, when the param is loaded it’s being converted into a Python dict and then into a string. Within the module:

&nbsp;&nbsp;&nbsp;&nbsp;trust\_policy=dict(type='str’),  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;trust\_policy = module.params['trust\_policy']  
&nbsp;&nbsp;&nbsp;&nbsp;print ‘PARROT’, type(trust\_policy), trust\_policy

Prints:

PARROT \<type ‘str’\> u'Version': u'2012-10-17', u'Statement': [{u'Action': u'sts:AssumeRole', u'Sid': u'', u'Effect': u'Allow', u'Principal': {u'Service': u'ec2.amazonaws.com'}}]}

If I leave off the type=‘str’, the file is loaded as a dict:

&nbsp;&nbsp;&nbsp;&nbsp;trust\_policy=dict(),  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;trust\_policy = module.params['trust\_policy']  
&nbsp;&nbsp;&nbsp;&nbsp;module.exit\_json(changed=True, tp=trust\_policy)

changed: [127.0.0.1] =\> {"changed": true, "tp": {"Statement": [{"Action": "sts:AssumeRole", "Effect": "Allow", "Principal": {"Service": "ec2.amazonaws.com"}, "Sid": ""}], "Version": "2012-10-17"}}

How can I prevent this conversion? I just want the raw file contents. debug msg=“{{ lookup(..) }}” does not display this behavior.

Regards,  
-scott

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 13, 2014, 11:18pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/2 "2014-04-13T23:18:51Z")

</div>

“However, when the param is loaded it’s being converted into a Python dict and then into a string. Within the module”

Nope, the file lookup plugin just returns strings.

You’d need to pass this to the “from\_json” filter most likely, but you know what’s an even better way to do this?

vars\_files or include\_vars!

This is exactly what they do, and they can also take JSON inputs.

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [April 14, 2014, 12:44am UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/3 "2014-04-14T00:44:07Z")

</div>

Yes, it’s a string when lookup lets go of it. However, once it’s a module param it’s converted from JSON into a dict. Notice this line:

> 

Lookup has loaded it as a string, but when I pull it out of the module params it has been 1) parsed as JSON and then 2) converted back into a string. See the u’…’ keys and values? That’s a JSON string that has been run through loads (converting it to a dict) and then back into a string representation of a dict.

I don’t want it as a dict or vars at all, just a raw string that happens to have JSON in it. I’m passing it to boto as a policy file. vars or include\_vars is exactly what I _don’t_ want. 🙂

Regards,  
-scott

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [April 14, 2014, 1:53pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/4 "2014-04-14T13:53:56Z")

</div>

Note that the ec2\_lc module has the same issue with the user\_data field. When I was using it to put a shell script in user\_data it worked fine. Now that I’ve changed to using JSON, I get this in user\_data instead of the raw JSON that I need:

{u’app\_environment’: u’stage’, u’database\_name’: u’sp\_app’, u’database\_endpoint\_port’: u’3306’, u’database\_endpoint\_host’: u’sp-[stage-rds-instance-master.parrot.us](http://stage-rds-instance-master.parrot.us)-[west-1.rds.amazonaws.com](http://west-1.rds.amazonaws.com)’}

- name: Create new launch configuration  
local\_action:  
module: ec2\_lc  
name: “{{ environment\_web\_launch\_configuration\_name }}”  
state: present  
user\_data: “{{ lookup(‘template’, ‘…/templates/user\_data.json’) }}”  
[…]

This is happening after the lookup runs, but before the module extracts the parameters.

Regards,  
-scott

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 9:57pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/5 "2014-04-15T21:57:21Z")

</div>

```auto

Doesn't seem like it to me:

From ec2_lc:

            user_data=dict(type='str'),

There is nothing in the module code that coerces the type.

```

Some fields do coerce inputs, like the tags field on the ec2 module. This one does not.

---

<div class="post-metadata">

**Author:** ![Scott\_Anderson](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@Scott\_Anderson](https://forum.ansible.com/u/Scott_Anderson)\
**Post date:** [April 15, 2014, 10:46pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/6 "2014-04-15T22:46:00Z")

</div>

Here’s a test case.

The problem is only displayed in actions (or local\_actions, not shown below but I did test it) with JSON in parameters that have variable substitutions in them. There’s definitely a different path being followed given the REMOTE\_MODULE lines are different.

test\_params module:

#!/usr/bin/python

def main():  
module = AnsibleModule(  
argument\_spec=dict(  
thing=dict(type=‘str’),  
)  
)

thing = module.params.get(‘thing’)

module.exit\_json(changed=False, thing=thing)

# import module snippets

from ansible.module\_utils.basic import \*

main()

pb\_test.yml:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [April 15, 2014, 10:53pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/7 "2014-04-15T22:53:04Z")

</div>

Great! Very useful.

Can you please file this as a bug report so we don’t lose these steps.

Thanks!!!

---

<div class="post-metadata">

**Author:** ![Vyacheslav\_Kuznetsov](https://avatars.discourse-cdn.com/v4/letter/v/82dd89/32.png) [@Vyacheslav\_Kuznetsov](https://forum.ansible.com/u/Vyacheslav_Kuznetsov)\
**Post date:** [December 30, 2014, 12:17pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/8 "2014-12-30T12:17:00Z")

</div>

Hi,

We stumbled upon the same bug: you cannot form a valid JSON as user\_data using ec2\_lc module. It sets the single quotes instead of double ones ☹

We have to encode user\_data to Base64 to work around the bug. Probably it can be helpful to others.

Kind regards,  
Vyacheslav.

---

<div class="post-metadata">

**Author:** ![Sudarshan\_Acharya](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sudarshan\_Acharya](https://forum.ansible.com/u/Sudarshan_Acharya)\
**Post date:** [April 7, 2015, 5:56pm UTC](https://forum.ansible.com/t/module-params-doing-json-conversion/15008/9 "2015-04-07T17:56:39Z")

</div>

Was a bug ever filed?

I am coming across the same bug. All I need is to read the raw contents of a file and that just happens to have json in it. But AnsibleModule has the read the content as dict and hence I am unable to get the raw string.
