# creating yaml dictionary, converting to json and using in uri module causes UnboundLocalError

**URL:** <https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418>\
**Category:** Ansible Project\
**Created:** [January 24, 2014, 4:10pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418 "2014-01-24T16:10:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim1](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@jim1](https://forum.ansible.com/u/jim1)\
**Post date:** [January 24, 2014, 4:10pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418/1 "2014-01-24T16:10:10Z")

</div>

So I’m able to create the dictionary, and I can convert it to JSON in a debug statement, but when I try to convert it to JSON in the URI module it throws an unhandled exception. What’s going on here?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 24, 2014, 4:29pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418/2 "2014-01-24T16:29:05Z")

</div>

ansible --version ?

Also not that this means it shouldn’t raise an error but, this is easier:

debug: var=stash\_json

---

<div class="post-metadata">

**Author:** ![jim1](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@jim1](https://forum.ansible.com/u/jim1)\
**Post date:** [January 24, 2014, 6:08pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418/3 "2014-01-24T18:08:48Z")

</div>

ansible 1.4.4

My confusion is that:

msg: ‘{{ stash\_json | to\_json }}’

works just fine, so it’s not the to\_json filter in and of itself that’s causing the bug. It’s using it in the uri module (or maybe just the body parameter).

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 24, 2014, 9:13pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418/4 "2014-01-24T21:13:46Z")

</div>

Actually, no, the above traceback does not have anything to do with the URI module.

Please make sure there’s a github filed on it though as I’d like to understand the cause of it.

Thanks!

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [January 26, 2014, 9:41pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418/5 "2014-01-26T21:41:46Z")

</div>

This is a similar stack trace triggered by the same Python syntax  
error noted in:

[https://github.com/ansible/ansible/issues/5566](https://github.com/ansible/ansible/issues/5566)

Unfortunately the syntax error masks the real issue.

Does it work if you '\>' escape the jason string as follows?

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stash\_json: \>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ text: "some thing" }

K

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 26, 2014, 10:22pm UTC](https://forum.ansible.com/t/creating-yaml-dictionary-converting-to-json-and-using-in-uri-module-causes-unboundlocalerror/14418/6 "2014-01-26T22:22:54Z")

</div>

To avoid confusion, this is not escaping. This is a “folded block”, which is a way of inputting data over multiple lines.

[http://yaml.org/spec/1.2/spec.html#style/block/folded](http://yaml.org/spec/1.2/spec.html#style/block/folded)

–Michael
