In python the Jinja2 template returns a backslash in front of a double quote, I need to remove that

3.3k Views Asked by At

One of the lines in my jinja2 template needs to return

   STACKNAME=\"",{"Ref":"AWS::StackName"},"\"

Putting the above into the template returns

   STACKNAME=\\\"\",{\"Ref\":\"AWS::StackName\"},\"\\\"

I tried creating a variable

 DQ = '"'

and setting the template as

STACKNAME="{{DQ}},{{{DQ}}Ref{{DQ}}:{{DQ}}AWS::StackName{{DQ}}},{{DQ}}"

but the result still puts a backslash in front of the {{DQ}} variable

I also tried putting in a unique string %%%DQ%%% and then getting the results and then doing a string replace but it still gives me the backslash.

How do I get the results I want?

UPDATE: My apologies. It turns out that it is not the jinja2 template that is returning the escaped quotes. I am making a later call in the script to:

lc.UserData=Base64(Join("", [commandList]))

And it is this call to the Troposphere Module for Base64 and/or Join that is causing the problem and inserting the escapes.

Testing Further shows specifically that it is Base64 that does the escaping.

1

There are 1 best solutions below

0
On

This feels like a hack and I hope someone has a better solution but I solved the problem by doing the following.

In the template, I made the line look like this:

STACKNAME="QQ,{QQRefQQ:QQAWS::StackNameQQ},QQ"

Then, in the last line of the program where I currently have:

print t.to_json()

I changed it to

print t.to_json().replace("QQ", '"')

Which produces exactly what I'm looking for.