Jinja2 converting double quotes to single quotes

10,579 views
Skip to first unread message

RobL

unread,
Apr 30, 2014, 3:02:05 PM4/30/14
to ansible...@googlegroups.com
Hi:

I'm trying to use the template module to create a JSON file.

I'm hitting an issue where some of my double-quotes are converting to single quotes.  Is there a way to ensure that Jinja2 won't do this conversion?  Notice how output.json has double-quotes for the first variable but single-quotes for the second variable.  I've tried various ways of quoting, both in the template and in values.json.

Sample hitting the issue:

output.json:
{
    "keepDoubleQuotes": [ {"name": "value"} ],
    "loseDoubleQuotes": [{'name2': 'value2nestedValue'}]
}

test/tasks/main.yml:
---
    - name: dbg
      debug: var=keepDoubleQuotes

    - name: dbg2
      debug: var=loseDoubleQuotes

    - name: template
      template: src="test.j2"
                dest="/var/tmp/output.json"

test/templates/test.j2:
{
    "keepDoubleQuotes": {{ keepDoubleQuotes}},
    "loseDoubleQuotes": {{ loseDoubleQuotes}}
}

test/values.json:
{
 keepDoubleQuotes: '[
    {"name": "value"}
    ]',
 "nestedVar": "nestedValue",
 loseDoubleQuotes: '[
    { "name2": "value2{{ nestedVar }}" }
    ]',
}

command line:
ansible-playbook  -e "@test/values.json" -vvvv  ./test.yml

Output:
TASK: [test | dbg] ************************************************************ 
ok: [...] => {
    "item": "", 
    "keepDoubleQuotes": [
        {
            "name": "value"
        }
    ]
}

TASK: [test | dbg2] *********************************************************** 
ok: [...] => {
    "item": "", 
    "loseDoubleQuotes": [
        {
            "name2": "value2nestedValue"
        }
    ]
}




Michael DeHaan

unread,
May 1, 2014, 5:25:41 PM5/1/14
to ansible...@googlegroups.com
This is hard for me to follow where you have defined these variables, how about sharing everything in a gist?




--
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/1ee39758-80a7-48fe-a2ca-059b11a6c000%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

RobL

unread,
May 2, 2014, 12:17:17 PM5/2/14
to ansible...@googlegroups.com
https://gist.github.com/anonymous/1ca1385f46a688ed2561

The output I see is:
"loseDoubleQuotes": [{'name2': 'value2nestedValue'}]

What I expect to see is:
"loseDoubleQuotes": [{"name2": "value2nestedValue"}]

Thanks for looking.

Rob

Brice Burgess

unread,
May 2, 2014, 2:11:23 PM5/2/14
to ansible...@googlegroups.com
RobL,

Can you use ansible's regex_replace jinja filter in test.j2 to turn surrounding single quotes to double quotes? e.g.

regex_replace('^[\'"]?(.*)[\'"]?$','"\\1"')



```
{
"keepDoubleQuotes": {{ keepDoubleQuotes | regex_replace('^[\'"]?(.*)[\'"]?$','"\\1"') }},
"loseDoubleQuotes": {{ loseDoubleQuotes | regex_replace('^[\'"]?(.*)[\'"]?$','"\\1"')}}
}
```

I didn't test that the matching single quote is escaped with \

 


See also ansible's regex_filter

Michael DeHaan

unread,
May 2, 2014, 3:15:38 PM5/2/14
to ansible...@googlegroups.com
Just as a quick PSA, you can actually use YAML for parameters passed in to "-e @test/values" if you like.

In the above example, nestedVar is expressed as a string, but that string actually contains no quotes, so that's why you are not seeing quotes there.

The easiest suggestion here if you are trying to write a datastructure as json, is to have a template that is exactly this:

{{ datastructure | to_json }}

And it will do the right thing and use the kind of quoting that JSON entails.

This also has the added benefit of keeping everything in nice pure datastructures and not having to write something in a format for a particular datastructure.

By no means should you have to worry about regex replacements to solve this, that would be horribly painful :)




RobL

unread,
May 2, 2014, 7:20:10 PM5/2/14
to ansible...@googlegroups.com
Thank you!  That fixed the problem.
Reply all
Reply to author
Forward
0 new messages