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

5,604 views
Skip to first unread message

j...@rybarski.com

unread,
Jan 24, 2014, 11:10:10 AM1/24/14
to ansible...@googlegroups.com
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?

---
- name: testguy
  gather_facts: no
  sudo: no
  hosts: local

  tasks:
  - set_fact:
      stash_json: { text: "some thing" }
  - debug:
      msg: '{{ stash_json | to_json }}'
  - uri: >
      method=POST
      body='{{ stash_json | to_json }}'
      return_content=yes
      HEADER_Content-Type="application/json"



$ ansible-playbook -i hosts test.yml

PLAY [test] **************************************************************** 

TASK: [set_fact ] ************************************************************* 
ok: [localhost]

TASK: [debug ] **************************************************************** 
ok: [localhost] => {
    "msg": {
        "text": "some thing"
    }
}
Traceback (most recent call last):
  File "/tmp/jim/env/bin/ansible-playbook", line 269, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/tmp/jim/env/bin/ansible-playbook", line 209, in main
    pb.run()
  File "/tmp/jim/env/local/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 263, in run
    if not self._run_play(play):
  File "/tmp/jim/env/local/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 592, in _run_play
    if not self._run_task(play, task, False):
  File "/tmp/jim/env/local/lib/python2.7/site-packages/ansible/playbook/__init__.py", line 357, in _run_task
    self.callbacks.on_task_start(template(play.basedir, name, task.module_vars, lookup_fatal=False, filter_fatal=False), is_handler)
  File "/tmp/jim/env/local/lib/python2.7/site-packages/ansible/utils/template.py", line 319, in template
    varname = template_from_string(basedir, varname, vars, fail_on_undefined)
  File "/tmp/jim/env/local/lib/python2.7/site-packages/ansible/utils/template.py", line 543, in template_from_string
    return res
UnboundLocalError: local variable 'res' referenced before assignment

Michael DeHaan

unread,
Jan 24, 2014, 11:29:05 AM1/24/14
to ansible...@googlegroups.com
ansible --version ?

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

debug: var=stash_json




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Michael DeHaan <mic...@ansibleworks.com>
CTO, AnsibleWorks, Inc.
http://www.ansibleworks.com/

j...@rybarski.com

unread,
Jan 24, 2014, 1:08:48 PM1/24/14
to ansible...@googlegroups.com
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).

Michael DeHaan

unread,
Jan 24, 2014, 4:13:46 PM1/24/14
to ansible...@googlegroups.com
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!

Kahlil Hodgson

unread,
Jan 26, 2014, 4:41:46 PM1/26/14
to ansible...@googlegroups.com
This is a similar stack trace triggered by the same Python syntax
error noted in:

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:
stash_json: >
{ 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

Michael DeHaan

unread,
Jan 26, 2014, 5:22:54 PM1/26/14
to ansible...@googlegroups.com
To avoid confusion, this is not escaping.  This is a "folded block", which is a way of inputting data over multiple lines.


--Michael

Reply all
Reply to author
Forward
0 new messages