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 filter could not keep non-ascii characters #68702

Closed
tumluliu opened this issue Apr 5, 2020 · 5 comments
Closed

to_json filter could not keep non-ascii characters #68702

tumluliu opened this issue Apr 5, 2020 · 5 comments
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. python3 support:community This issue/PR relates to code supported by the Ansible community.

Comments

@tumluliu
Copy link

tumluliu commented Apr 5, 2020

SUMMARY

to_json/to_nice_json filters could not properly preserve non-ascii characters in variables

ISSUE TYPE
  • Bug Report
COMPONENT NAME

to_json/to_nice_json filters

ANSIBLE VERSION
ansible 2.9.2
  config file = /home/me/to/my/project/ansible.cfg
  configured module search path = ['/home/me/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/me/.pyenv/versions/3.7.4/lib/python3.7/site-packages/ansible
  executable location = /home/me/.pyenv/versions/3.7.4/bin/ansible
  python version = 3.7.4 (default, Jul 19 2019, 16:11:19) [GCC 7.4.0]
CONFIGURATION
NSIBLE_SSH_ARGS = -o ServerAliveInterval=30 -o ControlMaster=auto -o ControlPersist=60s -o UserKnownHostsFile=/dev/null
DEFAULT_CALLBACK_WHITELIST = ['profile_tasks']
DEFAULT_FORKS = 10
DEFAULT_TIMEOUT = 30
HOST_KEY_CHECKING = False
OS / ENVIRONMENT

Ubuntu 18.04

STEPS TO REPRODUCE

test-playbook.yml:

- name: Test templating with unicodes
  hosts: localhost
  connection: local
  become: no
  tasks:
  - name: Template a json file with Chinese and German chars
    vars:
      german_var:
        a: München
        b: Öüä
    template:
      src: "test_template.txt.j2"
      dest: "test.txt"

test_template.txt.j2:

{
    "germanVar": {{ german_var | to_json }}
}
---
german_var: {{ german_var | to_yaml }}
EXPECTED RESULTS
{
    "germanVar": {"a": "München", "b": "Öüä"}
}
---
german_var: {a: München, b: Öüä}
ACTUAL RESULTS
{
    "germanVar": {"a": "M\u00fcnchen", "b": "\u00d6\u00fc\u00e4"}
}
---
german_var: {a: München, b: Öüä}

Is this actual behavior by design? If yes, is it possible to stop it from doing that converting while to keep the raw non-ascii characters, just as what to_yaml does?

@ansibot
Copy link
Contributor

ansibot commented Apr 5, 2020

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:community This issue/PR relates to code supported by the Ansible community. labels Apr 5, 2020
@mkrizek
Copy link
Contributor

mkrizek commented Apr 6, 2020

def to_json(a, *args, **kw):
''' Convert the value to JSON '''
return json.dumps(a, cls=AnsibleJSONEncoder, *args, **kw)

This appears to be due to ensure_ascii arg for json.dumps [0] is True by default which we do not change in to_json filter. Changing that would fix the issue however I am not sure what would be the implications of such change.

In the meantime however you can probably work around that by doing the following:

{{ non_ascii_var | to_json(ensure_ascii=False) }}

[0] https://docs.python.org/3/library/json.html#json.dump

@tumluliu
Copy link
Author

tumluliu commented Apr 6, 2020

Thank you so much @mkrizek !! The ensure_ascii=False works like a charm. For me, this can be closed. But it would be great to have this "hidden" param documented somewhere in Ansible

@bmillemathias
Copy link
Contributor

would also require some unit/integration test also, right ?

acozine pushed a commit that referenced this issue Jul 30, 2020
samccann pushed a commit to samccann/ansible that referenced this issue Aug 6, 2020
acozine added a commit that referenced this issue Aug 7, 2020
* Misc typo fixes (#71089)

(cherry picked from commit 504ef60)

* Add some documentation for the format of meta/runtime.yml (#71035)

* Document the format of meta/runtime.yml

* Document multiple Ansible versions

Clarify difference between deprecation and tombstone fields

* add note

(cherry picked from commit a9eb8b0)

* add note to uninstall older versions of ansible for pip (#71023)

* add note to uninstall older versions of ansible for pip

* combine with the other PR

(cherry picked from commit 72d3d44)

* VMware: Inventory scenario guide for hostnames (#71055)

Added a scenario guide for ``hostnames`` parameter
for vmware_vm_inventory.

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
(cherry picked from commit 0055673)

* Document string tests a bit more (#71049)

- Explain how `regex` differs from `match` and `search`.
- Document `multiline` and `ignorecase`.

Signed-off-by: Rick Elrod <rick@elrod.me>
(cherry picked from commit 701c638)

* docs: Add a note about package requirements for fact gathering (#70796)

Fixes: #26148

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
(cherry picked from commit a6725d6)

* added note about fakeroot (#71018)

see #70895

(cherry picked from commit 11a31e9)

* Update documentation of httpapi's handle_httperror method for clarity (#70991)

(cherry picked from commit a0523e5)

* DOCS: add 2.10 collections roadmap (#70975)

* draft of 2.10 collections roadmap

* incorporates feedback from felixfontein

* gundalow and samccann feedback, fix link

Co-authored-by: Alicia Cozine <acozine@users.noreply.github.com>
(cherry picked from commit 9879da8)

* updates changelog types; some updates for easier translation (#71027)

Co-authored-by: Alicia Cozine <acozine@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 4f4436c)

* Document common return values with examples (#71046)

* adding return value examples
* shift to console code blocks
* cleaning up whitespace and shortening invocation example
* reordering diff section

(cherry picked from commit 864573a)

* Update intro_getting_started.rst (#71039)

Added two additional learning resources in the See also: section- forgot closing backticks

(cherry picked from commit 9850915)

* Guide users to use ansible-runner (#71063)

Update the docs to guide users to use `ansible-runner` instead of using Python API directly. In many use cases, executing Ansible playbooks are sufficient. In those use cases, `ansible-runner` is easier and much stable to use comparing with Python API, but there is no mention of it.

(cherry picked from commit 0c855dc)

* Porting guides for ansible-base 2.10 and ansible 2.10 (#70891)

* Fix changelog link title.

* Rename Ansible 2.10 and 2.11 porting guides to Ansible-base porting guides.

* Add stub for automatically generated 2.10 porting guide.

* Move things that should not be in the ansible-base porting guide to the ansible porting guide.

* Apply changes to base porting guides.

* Add remark that ansible-base is mainly for developers.

* Ansible Base -> Ansible-base

* Fix link in base porting guide.

* Add generated porting guide.

* Use same header signs as antsibull-changelog's RST builder.

* Update generated porting guide.

(cherry picked from commit 61b36c6)

* Update network platform guides with FQCN (#70699)

* fqcn all the docs things!

(cherry picked from commit 54bee71)

* Document how to upgrade to ansible with pip (#70768)

Fixes #70348

(cherry picked from commit 5019335)

* document how to migrate between collections (#70243)

* document how to migrate between collections
* Apply suggestions from code review

Co-authored-by: John R Barker <john@johnrbarker.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 58145df)

* remove github link from plugins (#70951)

(cherry picked from commit e28b20d)

* Add latest rc from ansible-base (#70974)

* Add latest rc from ansible-base

(cherry picked from commit d62dffa)

* Document to_json will convert to ASCII strings by default (#70954)

... as reported in issue #68702

(cherry picked from commit 8c48366)

* Update the porting guide for ansible-2.10.0a8 (#71141)

(cherry picked from commit 0a9638c)

Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
Co-authored-by: Sloane Hertel <shertel@redhat.com>
Co-authored-by: Rick Elrod <rick@elrod.me>
Co-authored-by: Brian Coca <bcoca@users.noreply.github.com>
Co-authored-by: Nathaniel Case <ncase@redhat.com>
Co-authored-by: Alicia Cozine <879121+acozine@users.noreply.github.com>
Co-authored-by: Terciero <terciero@users.noreply.github.com>
Co-authored-by: Brendon O'Sullivan <49501251+bjosullivan@users.noreply.github.com>
Co-authored-by: EthanHur <ethan0311@gmail.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Baptiste Mille-Mathias <baptiste.millemathias@gmail.com>
Co-authored-by: Toshio Kuratomi <a.badger@gmail.com>
zoredache pushed a commit to zoredache/ansible that referenced this issue Aug 10, 2020
@mkrizek
Copy link
Contributor

mkrizek commented Aug 27, 2020

Closing as the functionality has been documented in the above PR.

@mkrizek mkrizek closed this as completed Aug 27, 2020
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Aug 27, 2020
@ansible ansible locked and limited conversation to collaborators Sep 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. python3 support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

No branches or pull requests

5 participants