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 <dyiou...@gmail.com> 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 
ansible-project+unsubscr...@googlegroups.com<mailto:ansible-project+unsubscr...@googlegroups.com>.
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/6D8F3276-7A70-4ABC-A47D-B177ACB4E34C%40nist.gov.

Reply via email to