How to escape single quotes within single quotes in Ansible

3,854 views
Skip to first unread message

Shifa Shaikh

unread,
Feb 26, 2020, 2:05:41 AM2/26/20
to Ansible Project
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(',') }}"




Stefan Hornburg (Racke)

unread,
Feb 26, 2020, 2:12:57 AM2/26/20
to ansible...@googlegroups.com
On 2/26/20 8:05 AM, Shifa Shaikh wrote:
> Below is my playbook:
>
> |
>    -set_fact:
What about the quote filter?

https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#id8

Regards
Racke

>
>
> --
> 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 <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/b857c17b-00f3-44bb-8fd8-4d08f597b6ce%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/b857c17b-00f3-44bb-8fd8-4d08f597b6ce%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Shifa Shaikh

unread,
Feb 26, 2020, 2:24:24 AM2/26/20
to Ansible Project
@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('') }}"
Reply all
Reply to author
Forward
0 new messages