# problem quoting string

**URL:** <https://forum.ansible.com/t/problem-quoting-string/37677>\
**Category:** Ansible Project\
**Created:** [February 20, 2023, 3:03pm UTC](https://forum.ansible.com/t/problem-quoting-string/37677 "2023-02-20T15:03:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregory\_Edigarov](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Gregory\_Edigarov](https://forum.ansible.com/u/Gregory_Edigarov)\
**Post date:** [February 20, 2023, 3:03pm UTC](https://forum.ansible.com/t/problem-quoting-string/37677/1 "2023-02-20T15:03:54Z")

</div>

Hello,

having something like this in my playbook  
vars:  
somevar:  
command:

- run: |  
“somecommand ‘somestring’ | awk ‘{print $2}’”

tasks:

- name: output var to file  
copy:  
content: “{{ somevar | to\_nice\_yaml(sort\_keys=false,indent=2) }}”  
dest: /tmp/tempfile

when I look at /tmp/tempfile it looks like ansible doubles apostrophes:

command:  
– run: ‘"somecommand ‘‘somestring’’ | awk ‘’{print $2}’'"

is there any way to make ansible produce file without extra apostrophes?

thanks.

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [February 20, 2023, 3:41pm UTC](https://forum.ansible.com/t/problem-quoting-string/37677/2 "2023-02-20T15:41:43Z")

</div>

When I try that code I get something different altogether, note the  
single dash for the list item:

~$ cat /tmp/tempfile  
command:  
- run: '"somecommand ''somestring'' | awk ''{print $2}''"

&nbsp;&nbsp;&nbsp;&nbsp;'

But this seems OK as that is what ansible is instructed to do.

So, the better question is what are you trying to do?  
What do you want that file to look like - exactly?

Also, you ask for yaml, and when that is emitted, ansible (or the yaml  
library) emits whatever is needed to make it valid YAML.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 20, 2023, 4:56pm UTC](https://forum.ansible.com/t/problem-quoting-string/37677/3 "2023-02-20T16:56:59Z")

</div>

> Hello,
> 
> having something like this in my playbook  
> vars:  
> &nbsp;&nbsp;somevar:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- run: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"somecommand 'somestring' | awk '{print $2}'"
> 
> tasks:  
> &nbsp;&nbsp;&nbsp;- name: output var to file  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ somevar | to\_nice\_yaml(sort\_keys=false,indent=2) }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/tempfile
> 
> when I look at /tmp/tempfile it looks like ansible doubles apostrophes:  
> command:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-- run: '"somecommand ''somestring'' | awk ''{print $2}''"
> 
> is there any way to make ansible produce file without extra apostrophes?
> 
> thanks.

Let's simplify the variable for testing

&nbsp;&nbsp;cmd: |  
&nbsp;&nbsp;&nbsp;&nbsp;"somecommand 'somestring'"

1) The filters \*to\_yaml\* and \*to\_nice\_yaml\* will quote the strings

&nbsp;&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/tempfile  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ cmd|to\_yaml }}"

gives (note the empty line and the closing single-quote

> cat /tmp/tempfile

'"somecommand ''somestring''"

&nbsp;&nbsp;'

Remove the trailing new line by adding the dash to the block

&nbsp;&nbsp;cmd: |-  
&nbsp;&nbsp;&nbsp;&nbsp;"somecommand 'somestring'"

now the task creates the file

> cat /tmp/tempfile

'"somecommand ''somestring''"'

In a single-quoted block, the single-quote is the only character that  
have to be escaped; by itself. This is the reason of the 'extra  
apostrophes'.

2) Add the dash to your block to remove the new line

&nbsp;&nbsp;somevar:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- run: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"somecommand 'somestring' | awk '{print $2}'"

The filter \*to\_nice\_yaml\* will quote the string

&nbsp;&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/tempfile  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: "{{ somevar|to\_nice\_yaml }}"

gives valid YAML with the list \*command\* and single item

> cat /tmp/tempfile

command:  
- run: '"somecommand ''somestring'' | awk ''{print $2}''"'

Read the file and use the command

&nbsp;&nbsp;&nbsp;&nbsp;- include\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file: /tmp/tempfile  
&nbsp;&nbsp;&nbsp;&nbsp;- command: "echo {{ command.0.run }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: out  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: out.stdout

gives probably what you want

&nbsp;&nbsp;out.stdout: somecommand 'somestring' | awk '{print $2}'

The run-string was properly expanded and used in the command line.

3) If you really want to get rid of the 'extra apostrophes' you'll  
have to expand the strings in the template. For example,

&nbsp;&nbsp;&nbsp;&nbsp;- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/tempfile  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for key,val in somevar.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ key }}:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for i in val %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for k,v in i.items() %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {{ k }}: {{ v }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

gives

> cat /tmp/tempfile

command:  
- run: "somecommand 'somestring' | awk '{print $2}'"

Note that now the double-quotes are not part of the string stored  
in command.0.run

HTH,
