Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_json not working on variable #9903

Closed
dlip opened this issue Dec 28, 2014 · 4 comments
Closed

to_json not working on variable #9903

dlip opened this issue Dec 28, 2014 · 4 comments
Labels
bug This issue/PR relates to a bug.

Comments

@dlip
Copy link

dlip commented Dec 28, 2014

When running ansible v1.8.2, in the below playbook, to_json fails to handle a basic variable

- name: a play that runs entirely on the ansible host
  hosts: localhost
  vars:
     users:
       - bob
       - joe
  tasks:
    # no problem
    - debug: msg="{{users|to_yaml}}"
    # ERROR: an unexpected type error occurred. Error was Undefined is not JSON serializable
    - debug: msg="{{users|to_json}}"

It was confirmed in the mailing list here:

https://groups.google.com/forum/#!msg/ansible-project/B1EnYrc7xBI/r0N9yQmtYj8J

@bcoca bcoca added P3 labels Dec 29, 2014
@seenaka
Copy link

seenaka commented Jan 26, 2015

I'm also getting blocked by this issue, even with hard-coded json in body: "{...}" this same exception is thrown:

failed: [127.0.0.1] => {"failed": true, "parsed": false}
Traceback (most recent call last):
  File "/Users/seena/.ansible/tmp/ansible-tmp-1422300547.12-154877083400880/uri", line 2010, in <module>
    main()
  File "/Users/seena/.ansible/tmp/ansible-tmp-1422300547.12-154877083400880/uri", line 403, in main
    resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
  File "/Users/seena/.ansible/tmp/ansible-tmp-1422300547.12-154877083400880/uri", line 302, in uri
    resp, content = h.request(url, method=method, body=body, headers=headers)     
  File "/Library/Python/2.7/site-packages/httplib2/__init__.py", line 1593, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/Library/Python/2.7/site-packages/httplib2/__init__.py", line 1335, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/Library/Python/2.7/site-packages/httplib2/__init__.py", line 1258, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 833, in _send_output
    self.send(message_body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 805, in send
    self.sock.sendall(data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not dict


FATAL: all hosts have already failed -- aborting

@abondis
Copy link
Contributor

abondis commented Apr 29, 2015

not sure if it helps, but I had this issue.
code was

- debug: var="{{my_var | to_json}}"

changed to the following and it works:

- name: Debugging json
  debug: var="{{my_var | to_json}}"

@abadger
Copy link
Contributor

abadger commented May 14, 2015

@seenaka I believe that your issue is different and solved by: ansible/ansible-modules-core#1011 (you'll need to get the updated module from the git repo as it's a new feature so it won't be released in the 1.9.x series. It will be released in the next major release.)

@abadger
Copy link
Contributor

abadger commented May 14, 2015

@dlip There were some changes to Undefined handling in 1.9.x so I just tried the playbook in the original post with 1.9.1. That seems to work fine:

$ ansible-playbook --version
ansible-playbook 1.9.1
  configured module search path = None
[pts/12@roan /srv/ansible/ansible-friday/lib/ansible/modules/core/network/basics]$ ansible-playbook test.yml

PLAY [a play that runs entirely on the ansible host] ************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [debug msg="{{users|to_yaml}}"] ***************************************** 
ok: [localhost] => {
    "msg": "[bob, joe]\n"
}

TASK: [debug msg="{{users|to_json}}"] ***************************************** 
ok: [localhost] => {
    "msg": "[\"bob\", \"joe\"]"
}

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

@abondis Since your issue went away when name: was added it's probably a different problem. Could you open a new ticket with more details (a playbook to reproduce, the version of anisble being used, etc)?

Closing This Ticket

Hi!

We believe changes in the 1.9.1 release should resolve this question or problem for you.

This will also be included in the next major release.

If you continue seeing any problems related to this issue, or if you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular
issue is resolved.

Thank you!

@abadger abadger closed this as completed May 14, 2015
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

6 participants