# serious issues with autoescaping quotes in variables with output

**URL:** <https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451>\
**Category:** Ansible Project\
**Created:** [May 30, 2019, 4:24pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451 "2019-05-30T16:24:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 30, 2019, 4:24pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/1 "2019-05-30T16:24:04Z")

</div>

Simple problem, apparently not a simple solution.

tasks:

- name: packages  
shell: dpkg -l | awk -F ’ ’ ‘{print “{"package":"”$1"", “"version":"”$2""}"}’  
register: bleh  
ignore\_errors: yes

- set\_fact:  
packages: “{{ bleh.stdout\_lines }}”

-name: always fails here no matter what  
local\_action: copy content=“{{ packages | to\_json }}” dest=“myfile” ####\<— this always prints garbage

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 30, 2019, 4:25pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/2 "2019-05-30T16:25:59Z")

</div>

Using `content` with `copy` should be used minimally. Instead you should use a real template, and the `template` module.

Typically speaking, I recommend only using `content` with static simple strings. If you are using templating in `content` you should probably use the `template` module.

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 30, 2019, 4:42pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/3 "2019-05-30T16:42:06Z")

</div>

Ok, so I have tried this.

local\_action: template src=“{{ myvarwithrawquotes }}” dest=“myfile”

and it blows up spectacularly.

I do not see how I can use template with a variable full of raw json to print that variable (a list) to the local filesystem without trashing the variable.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 30, 2019, 4:45pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/4 "2019-05-30T16:45:12Z")

</div>

You need an actual template file. Not supplying a template string to `src`.

Such a file (packages.json.j2) would have content that looks like:

{{ packages | to\_json }}

Then you would have a template task like:

- template:  
src: packages.json.j2  
dest: myfile.json  
delegate\_to: localhost

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 30, 2019, 5:04pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/5 "2019-05-30T17:04:17Z")

</div>

So I have tried this (templating). If I use packages.stdout, no matter what I put in the template, I still get garbage:  
“{  
{"a":"b"}\n  
{"c":"d"}\n  
.  
.  
}”

If I do this with stdout\_lines, I get:  
["

{"a":"b"},{"c":"d"},{"e":"f"}  
.  
.

"]

The source that went into the variable was:  
{“a”:“b”}  
{“c”:“d”}

This is the root of my problem. The json is not a native json from something like .results. It is json i wrote, and shoved into a variable, to be as printed plain text to a file. I don’t understand why the absolute only way this works is for me to local\_action: shell echo {{var}} \>\> file, using with\_items. Every thing I have tried prints this with my quotes in my variable as escaped, no matter if I use template or copy.

What am I doing wrong with my variable? Why does everything I try to do force escapes into the content? I tried {% var %} but that doesn’t seem to work, and I can’t see any way to output raw, untouched, unmangled variables.

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 30, 2019, 5:48pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/6 "2019-05-30T17:48:58Z")

</div>

So, after about ~8 hours on this simple task, it looks like there is absolutely no way to output a variable populated with strings to a file _without ansible escaping the quotes in it_, unless you loop with\_items and shell echo it out.

This is absurd. Frankly, I don’t understand how noone else seems to have run into this horribly simple problem.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 30, 2019, 6:05pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/7 "2019-05-30T18:05:09Z")

</div>

Because we don't pipe through a filter when we don't need to

local\_action: copy content="{{ bleh.stdout }}" dest="myfile"

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 30, 2019, 6:09pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/8 "2019-05-30T18:09:53Z")

</div>

A few things I’ll mention. Your shell command doesn’t work for me. Even after fixing the quoting, it includes lines that are not packages.

You would likely be better off using the `package_facts` module, and then writing out a template using that registered data.

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 30, 2019, 6:32pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/9 "2019-05-30T18:32:54Z")

</div>

Package\_facts doesn’t work all the time. See:

[https://github.com/ansible/ansible/issues/55850](https://github.com/ansible/ansible/issues/55850)

I am loathe to use yum list installed, and have to work on dealing with rpm -qa in the future, but for now yum deals with it.

Playbook below (again, no matter what I do the variables get printed to local disk with escapes)

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 31, 2019, 2:01pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/10 "2019-05-31T14:01:50Z")

</div>

Anyone have any idea why you just can’t write out the packages variable without all the escaping, as populated in the playbook below? Neither copy content nor template work, forcing the use of shell which consumes the escaped chars to “fix” ansibles output. Hundreds/thousands of lines as a line-by-line echo into a file are unacceptable. Writing to remote host and pulling is also a nonstarter, as our requirements are to do as little destructive (disk writes) processes as possible.

Hopefully someone understands what is going on here. 🙂

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 31, 2019, 2:05pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/11 "2019-05-31T14:05:38Z")

</div>

Have you read my post yesterday? I believe the answer is in there.

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 31, 2019, 2:29pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/12 "2019-05-31T14:29:28Z")

</div>

Unfortunately the answer is not in your post. I can dump it raw {{ packages }} or {{ dpkg\_out.stdout\_lines }}, with _no filter_, and it STILL pumps out escaped quotes, whether I use template, copy, lineinfile, blockinfile, or any other ansible output method that isn’t shell echo.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 31, 2019, 8:16pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/13 "2019-05-31T20:16:23Z")

</div>

Is it actually pumping out the escaped quotes or is it just the callback plugin escaping the json to fit in a json string. The default callback plugin in Ansible uses json to display the status so if the stdout return value contains json it will be escaped, i.e. “ will become \” and so on.

Try creating a file with the stdout result of your task and look at the file directly.

---

<div class="post-metadata">

**Author:** ![erik\_clark](https://avatars.discourse-cdn.com/v4/letter/e/ba8739/32.png) [@erik\_clark](https://forum.ansible.com/u/erik_clark)\
**Post date:** [May 31, 2019, 9:56pm UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/14 "2019-05-31T21:56:36Z")

</div>

It is actually pumping out escaped quotes... not just escaped quotes  
in text you would expect from debug or what have you. I stopped using  
set\_fact entirely and just called the parent variable.stdout and it  
worked. Im curious to know if this behavior is caused by something  
getting mangled by pushing the var into a fact, but to be honest I am  
just happy to have this work now.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 1, 2019, 8:17am UTC](https://forum.ansible.com/t/serious-issues-with-autoescaping-quotes-in-variables-with-output/30451/15 "2019-06-01T08:17:21Z")

</div>

> It is actually pumping out escaped quotes... not just escaped quotes  
> in text you would expect from debug or what have you. I stopped using  
> set\_fact entirely and just called the parent variable.stdout and it  
> worked. Im curious to know if this behavior is caused by something  
> getting mangled by pushing the var into a fact, but to be honest I am  
> just happy to have this work now.

Nothing is mangled, it's because of using the wrong variable(see the excerpt from the documentation bellow.)  
In all the set\_fact you have posted in this thread you have used stdout\_lines and not stdout as you can see from my copy-paste.

> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;packages: "{{ bleh.stdout\_lines }}"

> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;packages: "{{ dpkg\_out.stdout\_lines }}"  
> &nbsp;&nbsp;when:  
> &nbsp;&nbsp;&nbsp;&nbsp;- dpkg\_rc.rc == 0  
> &nbsp;&nbsp;&nbsp;&nbsp;- ansible\_facts.packages is not defined
> 
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;packages: '{{ rpm\_out.stdout\_lines }}'  
> &nbsp;&nbsp;when:  
> &nbsp;&nbsp;&nbsp;&nbsp;- dpkg\_rc.rc != 0  
> &nbsp;&nbsp;&nbsp;&nbsp;- ansible\_facts.packages is not defined

> From the documentation:

stdout [1]  
Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, etc). This field contains the normal output of these utilities.

stdout\_lines [2]  
When stdout is returned, Ansible always provides a list of strings, each containing one item per line from the original output.

So output.stdout\_lines[0] is the fist line, output.stdout\_lines[1] second line and so on.  
stdout\_lines is usually used when needing to loop over the result line by line.

[1] [https://docs.ansible.com/ansible/2.8/reference\_appendices/common\_return\_values.html#stdout](https://docs.ansible.com/ansible/2.8/reference_appendices/common_return_values.html#stdout)  
[2] [https://docs.ansible.com/ansible/2.8/reference\_appendices/common\_return\_values.html#stdout-lines](https://docs.ansible.com/ansible/2.8/reference_appendices/common_return_values.html#stdout-lines)
