output json variable to file

102 views
Skip to first unread message

Khuong Dinh Pham

unread,
May 14, 2014, 9:58:43 AM5/14/14
to ansible...@googlegroups.com
Hi

I have this:

  - some_action: ....
    register: jsonformat
  - debug: var=jsonformat
  - local_action: shell echo {{ jsonformat }} > output


The json in console is displayed fine from debug action but the output file is not a valid json. It has some escape and unicode values. How can i output a valid json document to a file from a variable?

thx in advance.

James Cammarata

unread,
May 14, 2014, 11:35:45 AM5/14/14
to ansible...@googlegroups.com
Use "{{ jsonformat|to_json }}" to force the format to json.

Sample test:

- hosts: localhost
  gather_facts: no
  vars:
    foo: '{"a": [1,2,"3"]}'
  tasks:
  - shell: echo "{{ foo|to_json }}" > /tmp/output

After running: 

$ cat /tmp/output:
{"a": [1,2,"3"]}



--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0ea3e3d2-61bd-4bee-8602-7da9c41e865d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Khuong Dinh Pham

unread,
May 14, 2014, 4:37:42 PM5/14/14
to ansible...@googlegroups.com
Hi

It's working fine. Thanks a lot.

Brian Coca

unread,
May 14, 2014, 4:38:36 PM5/14/14
to ansible...@googlegroups.com
the issue was probably the lack of quotes as unquoted json and shell don't mix well​, you can probably ignore the to_json.
Reply all
Reply to author
Forward
0 new messages