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.
---
- name: test
hosts: localhost
become: false
gather_facts: false
vars:
mystr: "['/First Datacenter/vm/Prod-SRM']"
tasks:
- debug: msg="{{ mystr | regex_replace(\"([\\[\\]'])\",'') }}"
% ansible-playbook -i localhost, foo.yml
PLAY [test]
************************************************************************************************************
TASK [debug]
***********************************************************************************************************
ok: [localhost] => {
"msg": "/First Datacenter/vm/Prod-SRM"
}
PLAY RECAP
*************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
skipped=0 rescued=0 ignored=0
The solution was found on this stack overflow page.
https://stackoverflow.com/questions/52495838/ansible-remove-the-single-quote-character-from-a-string
Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123
On Jan 10, 2024, at 9:27 AM, Rowe, Walter P. (Fed) <[email protected]> wrote:
Have you tried:
regex_replace('(\\[)|(\\])|(\\')', '')
Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123
On Jan 10, 2024, at 9:08 AM, Dimitri Yioulos <[email protected]> wrote:
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:
---
- hosts: all
become: false
gather_facts: false
vars_prompt:
- name: "vcenter_username"
prompt: "Enter your Vcenter username"
private: no
- name: "vcenter_password"
prompt: "Enter your VMware password"
private: yes
vars:
vcenter_hostname: vcenter1.mycompany.com
vcenter_datacenter: First Datacenter
tasks:
- name: Find virtual machine's folder name
vmware_guest_find:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ inventory_hostname }}"
validate_certs: False
delegate_to: localhost
ignore_errors: true
register: vm_facts
tags:
- folder
- name: Show folders
ansible.builtin.debug:
msg: "{{ vm_facts.folders }}"
tags:
- folder
- name: Remove all snapshots of a VM
community.vmware.vmware_guest_snapshot:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ vcenter_datacenter }}"
folder: "{{ vm_facts.folders | regex_replace('(\\[)|(\\])', '') }}"
name: "{{ inventory_hostname }}"
state: remove_all
validate_certs: False
delegate_to: localhost
Previous to my having employed regex_replace, this is the output I'd get, e.g.,
for a vm's folder:
"['/First Datacenter/vm/Prod-SRM']"
The problem with that is [' and ']. They can't be part pf the folder name.
After employing regex-replace, the output is:
" '/Bedford Datacenter/vm/Bedford-Prod-SRM' "
Try as I may, I've not been able to find a way to also remove the single quotes
(the double quotes can remain). I've tried appending to the regex_replace, but
every piece of code i've used has failed.
regex_replace('(\\[)|(\\])'|(addlcodehere), '') }}
Your kinf assistance requested.
--
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
[email protected]<mailto:[email protected]>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/34393455-5021-4342-89b4-de276d68ccd9n%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/34393455-5021-4342-89b4-de276d68ccd9n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/D4C8E154-6B7D-4D2A-9EEF-7E0ECE8CBC17%40nist.gov.