# How to escape single quotes within single quotes in Ansible

**URL:** <https://forum.ansible.com/t/how-to-escape-single-quotes-within-single-quotes-in-ansible/32328>\
**Category:** Ansible Project\
**Created:** [February 26, 2020, 7:05am UTC](https://forum.ansible.com/t/how-to-escape-single-quotes-within-single-quotes-in-ansible/32328 "2020-02-26T07:05:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [February 26, 2020, 7:05am UTC](https://forum.ansible.com/t/how-to-escape-single-quotes-within-single-quotes-in-ansible/32328/1 "2020-02-26T07:05:40Z")

</div>

Below is my playbook:

`

- set\_fact:  
excludefolders: “{{ excludefolders + ’ -o -name ’ + item | default(‘’) }}”  
with\_items: “{{ lookup(‘vars’, ‘EXCLUDE\_’ + Layer).split(‘,’) }}”

- debug:  
msg: “excludedfolder is {{ excludefolders }}”  
`

I get the below output

```auto

ok: [10.0.17.113] => { "msg": "excludedfolder is -o -name custom -o -name tree -o -name log"

```

However i want the variable to have single quotes around item like below:

Expected output:

```auto

ok: [10.0.17.113] => { "msg": "excludedfolder is -o -name 'custom' -o -name 'tree' -o -name 'log'"

```

I tried to use escape charecter for single quotes but none of them worked. Below is what tried.

`

- set\_fact:  
excludefolders: “{{ excludefolders + ’ -o -name ’ + ' + item + ' | default(‘’) }}”  
with\_items: “{{ lookup(‘vars’, ‘EXCLUDE\_’ + Layer).split(‘,’) }}”

- set\_fact:  
excludefolders: “{{ excludefolders + ’ -o -name ’ ~ ' + item ~ ' | default(‘’) }}”  
with\_items: “{{ lookup(‘vars’, ‘EXCLUDE\_’ + Layer).split(‘,’) }}”

- set\_fact:  
excludefolders: “{{ excludefolders + ’ -o -name '’ + item +‘'’ | default(‘’) }}”  
with\_items: “{{ lookup(‘vars’, ‘EXCLUDE\_’ + Layer).split(‘,’) }}”

`

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 26, 2020, 7:12am UTC](https://forum.ansible.com/t/how-to-escape-single-quotes-within-single-quotes-in-ansible/32328/2 "2020-02-26T07:12:29Z")

</div>

> Below is my playbook:
> 
> \>  
> -set\_fact:  
> excludefolders:"{{ excludefolders + ' -o -name ' + item | default('') }}"  
> with\_items:"{{ lookup('vars', 'EXCLUDE\_' + Layer).split(',') }}"
> 
> -debug:  
> msg:"excludedfolder is {{ excludefolders }}"  
> \>
> 
> I get the below output
> 
> \>  
> ok:[10.0.17.113]=\>{"msg":"excludedfolder is -o -name custom -o -name tree -o -name log"  
> \>
> 
> However i want the variable to have single quotes around item like below:
> 
> Expected output:
> 
> \>  
> ok:[10.0.17.113]=\>{"msg":"excludedfolder is -o -name 'custom' -o -name 'tree' -o -name 'log'"  
> \>
> 
> I tried to use escape charecter for single quotes but none of them worked. Below is what tried.
> 
> \>  
> -set\_fact:  
> excludefolders:"{{ excludefolders + ' -o -name ' + \' + item + \' | default('') }}"  
> with\_items:"{{ lookup('vars', 'EXCLUDE\_' + Layer).split(',') }}"
> 
> -set\_fact:  
> excludefolders:"{{ excludefolders + ' -o -name ' ~ \' + item ~ \' | default('') }}"  
> with\_items:"{{ lookup('vars', 'EXCLUDE\_' + Layer).split(',') }}"
> 
> -set\_fact:  
> excludefolders:"{{ excludefolders + ' -o -name \'' + item +'\'' | default('') }}"  
> with\_items:"{{ lookup('vars', 'EXCLUDE\_' + Layer).split(',') }}"
> 
> \>

What about the quote filter?

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#id8](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#id8)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Shifa\_Shaikh](https://avatars.discourse-cdn.com/v4/letter/s/b2d939/32.png) [@Shifa\_Shaikh](https://forum.ansible.com/u/Shifa_Shaikh)\
**Post date:** [February 26, 2020, 7:24am UTC](https://forum.ansible.com/t/how-to-escape-single-quotes-within-single-quotes-in-ansible/32328/3 "2020-02-26T07:24:24Z")

</div>

@Stefan i tried using quote but it does not help. Can you confirm if this is the right way to use quote ? See below:  
`

excludefolders: “{{ excludefolders + ’ -o -name ’ + item | quote | default(‘’) }}”  
`
