# Trying to replace single quote with regex\_replace

**URL:** <https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [January 10, 2024, 2:08pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646 "2024-01-10T14:08:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [January 10, 2024, 2:08pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/1 "2024-01-10T14:08:45Z")

</div>

Hello, all.

I’m working with VMware modules in removing snapshots across all vCenter folders. In order for this to work, I first have to get the folders in which the virtual machines live. Here’s the playbook:

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 10, 2024, 2:27pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/2 "2024-01-10T14:27:09Z")

</div>

Have you tried:

**regex\_replace(‘(\[)|(\])|(\')’, ‘’)**

Walter

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [January 10, 2024, 2:56pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/3 "2024-01-10T14:56:37Z")

</div>

Hi, Walter.

Yes, and it fails:

**fatal: [api-dev-01 → localhost]: FAILED! =\> {**  
**“msg”: “template error while templating string: expected token ‘end of print statement’, got ‘string’. String: {{ vm\_facts.folders | regex\_replace(‘(\\[)|(\\])|(\\’)', ‘’) }}. expected token ‘end of print statement’, got ‘string’”**  
**}**

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 10, 2024, 3:14pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/4 "2024-01-10T15:14:17Z")

</div>

This works.

regex\_replace("([\[\]‘])",’')

Escaping the double quotes that define the search regex gets around needing to escape the single quote. Also note that I collapsed your search into a single character set.

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [January 10, 2024, 3:33pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/5 "2024-01-10T15:33:53Z")

</div>

Walter, that worked, and thanks so much! I both appreciate the regex solution, and it simplification. I also probably be able to reuse this, with some modifications, depending on what I’m trying to accomplish. Double win. Thanks again!

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [January 10, 2024, 4:03pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/6 "2024-01-10T16:03:17Z")

</div>

Walter, to further my knowledge, would you mind explaining further what this regex "([\[\]‘])",’ does? Is the regex specifi to regex\_replace?

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 10, 2024, 4:14pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/7 "2024-01-10T16:14:18Z")

</div>

In plain terms it is really this:

[']

The outside square brackets define a character set. The characters in between mean “any of these”.

Since square brackets define a character set we have to escape the inside square brackets to be see as characters in the set.

[']

Since this is ansible we have to escape the backslashes.

[\[\]']

You could place any characters in that set. The regex matches _any_ of them any number of times.

For example I could use [walter] and it would look for any of those characters.

You also can use ranges like [a-dv-z]. This all follows Python regex rules.

Walter

---

<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:** [January 10, 2024, 5:16pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/8 "2024-01-10T17:16:44Z")

</div>

> "['/First Datacenter/vm/Prod-SRM']"  
> The problem is [' and ']. They can't be part pf the folder name.

FWIW, convert the string to a list

s: "['/First Datacenter/vm/Prod-SRM']"  
l: "{{ s|from\_yaml }}"

gives

l.0: /First Datacenter/vm/Prod-SRM

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [January 10, 2024, 5:24pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/9 "2024-01-10T17:24:26Z")

</div>

Even better! Brilliant!

Walter

---

<div class="post-metadata">

**Author:** ![dyioulos](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dyioulos/32/8363_2.png) [@dyioulos](https://forum.ansible.com/u/dyioulos)\
**Post date:** [January 12, 2024, 5:05pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/10 "2024-01-12T17:05:16Z")

</div>

Thanks, Vladimir. Forgive my stupidity but, for my edification, how is that actually written in the playbook?

---

<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:** [January 12, 2024, 8:20pm UTC](https://forum.ansible.com/t/trying-to-replace-single-quote-with-regex-replace/38646/11 "2024-01-12T20:20:31Z")

</div>

> \> FWIW, convert the string to a list  
> \>  
> \> s: "['/First Datacenter/vm/Prod-SRM']"  
> \> l: "{{ s|from\_yaml }}"

> how is that actually written in the playbook?

For example,

> cat pb1.yml

- hosts: all  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;s: "['/First Datacenter/vm/Prod-SRM']"  
&nbsp;&nbsp;&nbsp;&nbsp;l: "{{ s|from\_yaml }}"  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: l|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: l.0

gives (abridged)

&nbsp;&nbsp;l\>type\_debug: list  
&nbsp;&nbsp;l.0: /First Datacenter/vm/Prod-SRM

But, the code in your first email shows you get \*vm\_facts\* as  
registered output of \*community.vmware.vmware\_guest\_find\*. Take a  
look at the Return Values of this module  
[https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware\_guest\_find\_module.html#return-values](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_find_module.html#return-values)

The documentation says \*folders\* is "list/elements=string". In this  
case, no conversion is needed to get the element of the list. For  
example,

> cat pb2.yml

- hosts: all  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;vm\_facts:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;folders: ['/First Datacenter/vm/Prod-SRM']  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: vm\_facts.folders|type\_debug  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: vm\_facts.folders.0

gives (abridged)

&nbsp;&nbsp;vm\_facts.folders|type\_debug: list  
&nbsp;&nbsp;vm\_facts.folders.0: /First Datacenter/vm/Prod-SRM

FWIW, try community.general.yaml callback. YAML is much easier to  
read compared to JSON. See  
[https://docs.ansible.com/ansible/latest/collections/community/general/yaml\_callback.html](https://docs.ansible.com/ansible/latest/collections/community/general/yaml_callback.html)
