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
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123

On Jan 10, 2024, at 11:03 AM, Dimitri Yioulos <dyiou...@gmail.com> wrote:

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

On Wednesday, January 10, 2024 at 10:33:53 AM UTC-5 Dimitri Yioulos wrote:
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!

On Wednesday, January 10, 2024 at 10:14:32 AM UTC-5 Rowe, Walter P. (Fed) wrote:
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<tel:(202)%20355-4123>

On Jan 10, 2024, at 9:27 AM, Rowe, Walter P. (Fed) <walte...@nist.gov> wrote:

Have you tried:

regex_replace('(\\[)|(\\])|(\\')', '')

Walter
--
Walter Rowe, Division Chief
Infrastructure Services Division
Mobile: 202.355.4123<tel:(202)%20355-4123>

On Jan 10, 2024, at 9:08 AM, Dimitri Yioulos <dyio...@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<http://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-proje...@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<mailto:ansible-project+unsubscr...@googlegroups.com>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d6b6bcd4-71fd-4193-acb7-fdd740ca7202n%40googlegroups.com<https://groups.google.com/d/msgid/ansible-project/d6b6bcd4-71fd-4193-acb7-fdd740ca7202n%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/386391D7-3331-4B32-A966-A5A1EA4B84A0%40nist.gov.

Reply via email to