# \[ansible-project \] getting unsafe ansible text when I trying to parse ansible register variable

**URL:** <https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [July 14, 2023, 7:17am UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223 "2023-07-14T07:17:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [July 14, 2023, 7:17am UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223/1 "2023-07-14T07:17:48Z")

</div>

Hi Groups ,

I am trying to parse the list variable but I am getting ansible unsafe text I need to match the vm name and get the corresponding vm group from the registered variable vm\_groups.  
tasks:

- name: “Gather DRS info about given Cluster”  
community.vmware.vmware\_drs\_group\_info:  
hostname: “{{ vcenter.hostname }}”  
username: “{{ vcenter.username }}”  
password: “{{ vcenter.password }}”  
validate\_certs: “{{ vcenter.validate\_certs }}”  
cluster\_name: “{{ vcenter\_env.cluster }}”  
delegate\_to: localhost  
register: group\_info

- name: set fact specific group\_info VM groups

ansible.builtin.set\_fact:  
match\_vm\_group: “{{ item.drs\_group\_info[vcenter\_env.cluster] | regex\_search(vm\_name) }}”  
when: “{{ item.drs\_group\_info[vcenter\_env.cluster] | regex\_search( vm\_name ) }}”  
loop: “{{ group\_info }}”

name: set fact specific VM group  
ansible.builtin.set\_fact:  
vm\_groups: “{{ group\_info.drs\_group\_info[vcenter\_env.cluster] | selectattr(‘type’, ‘==’, “vm” ) | list }}”

“vm\_groups”: "“[{‘group\_name’: VM2-on-dev’, ‘vms’: [‘deep-test’, ‘NED-DEV601-X’], ‘type’: ‘vm’}, {‘group\_name’: ‘VM2-on-uat’, ‘vms’: [‘NED-TST601-X’], ‘type’: ‘vm’}  
}

I appreciate your quick help .

Thanks  
Deepak

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [July 14, 2023, 10:50am UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223/2 "2023-07-14T10:50:20Z")

</div>

Please show us “group\_info” and “vm\_name”:

```auto
- name: Show registered var group_info and vm_name
  ansible.builtin.debug:
    msg:
      - "group_info: {{ group_info }}"
      - "vm_name: {{ vm_name }}"

```

It would be helpful to include the job log for the relevant section of your playbook as well. You say you are trying to parse “the list variable” but you don’t say what that variable is or show what it contains. Please help us help you.

---

<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:** [July 14, 2023, 11:14am UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223/3 "2023-07-14T11:14:10Z")

</div>

Adding to Todd’s request for more details … also look at json\_query vs selectattr if you are just filtering a JSON list on attribute value.

Walter

---

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [July 14, 2023, 12:32pm UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223/4 "2023-07-14T12:32:59Z")

</div>

Hi Todd,

Please find output below ansible task

```auto
- name: Show registered var group_info and vm_name
  ansible.builtin.debug:
    msg:
      - "group_info: {{ group_info }}"
      - "vm_name: {{ vm_name }}"

```

```auto

```

```auto
TASK [Show registered var group_info and vm_name] **************************************************************
ok: [NED-TST1x] => {
    "msg": [
        "group_info: {'failed': False, 'drs_group_info': {'NED-Cluster': [{'group_name': 'NED-HOST001-HG', 'hosts': ['ned-host612-hyp'], 'type': 'host'}, {'group_name': 'VM2-on-dev', 'vms': ['deep-test', 'NED-DEV601-X'], 'type': 'vm'}, {'group_name': 'VM2-on-uat', 'vms': ['NED-TST601-X'], 'type': 'vm'}]}, 'changed': False}",
        "vm_name: NED-TST601-X"
    ]
}

```

```auto

```

```auto
Thanks & Regards

```

```auto
Deepak Kumar

```

---

<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:** [July 14, 2023, 1:10pm UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223/5 "2023-07-14T13:10:57Z")

</div>

Look at your quoting here …

> name: set fact specific VM group  
> ansible.builtin.set\_fact:  
> vm\_groups: “{{ group\_info.drs\_group\_info[vcenter\_env.cluster] | selectattr(‘type’, ‘==’, “vm” ) | list }}”

That first and last quote are a “smart quotes” (leaning quotes). Your quotes around “vm” are double quotes.

1. You can’t have smart quotes. Change to normal double quotes.
2. You can’t have double quotes inside double quotes. Change “vm” to ‘vm’.

> name: set fact specific VM group  
> ansible.builtin.set\_fact:  
> vm\_groups: “{{ group\_info.drs\_group\_info[vcenter\_env.cluster] | selectattr(‘type’, ‘==’, ‘vm’ ) | list }}”

Walter

---

<div class="post-metadata">

**Author:** ![Deepak\_B\_K](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/deepak_b_k/32/5151_2.png) [@Deepak\_B\_K](https://forum.ansible.com/u/Deepak_B_K)\
**Post date:** [July 17, 2023, 10:59am UTC](https://forum.ansible.com/t/ansible-project-getting-unsafe-ansible-text-when-i-trying-to-parse-ansible-register-variable/38223/6 "2023-07-17T10:59:15Z")

</div>

Dear Walter,

Thank you very much for the timely help. I am all set with getting the correct group name out of the drs information .

Thanks  
Deepak Kumar
