Menu

#2 String quotes are unexpectly removed

closed
None
minor
bug
2023-11-14
2023-11-14
m2e
No

When I load a Ansible YAML like:

- set_fact:
    artifact_id: "{{ role_name }}"

and load it via.

yaml = YAML(typ='jinja2')
yaml.width = sys.maxsize
data = yaml.load("file.yml")

and store it via.

yaml.dump(data, "file.yml")

The string quotation is removed:

 - set_fact:
     artifact_id: {{ role_name }}

This is bad, because Ansible detects that as a syntax error.

Is there any way to prevent that?

Discussion

  • m2e

    m2e - 2023-11-14

    Used versions:

    ruamel.yaml==0.18.5
    ruamel.yaml.jinja2==0.2.7
    
     
  • Anthon van der Neut

    • status: open --> closed
     
  • Anthon van der Neut

    Please post these kind of questions on StackOverflow, that makes the answer much more easy to find for most people (tag them with ruamel.yaml and I will be notified automatically).

    Try to insert

    yaml.preserve_quotes = True
    

    after creating the YAML() instance and before loading.

     
  • Anthon van der Neut

    The officially recommended extension for files containing YAML documents has been .yaml since at least September 2006. So please catch up.

     
  • m2e

    m2e - 2023-11-14

    I searched already on Stack Overflow. But I did not expect to find the solution in the source code. Thanks for the answer.
    I'm also respecting the file extension in the future.

     

Log in to post a comment.

MongoDB Logo MongoDB