# Loop with Register and query output

**URL:** <https://forum.ansible.com/t/loop-with-register-and-query-output/28717>\
**Category:** Ansible Project\
**Tags:** azure\
**Created:** [October 4, 2018, 8:44am UTC](https://forum.ansible.com/t/loop-with-register-and-query-output/28717 "2018-10-04T08:44:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbmprivat](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@nbmprivat](https://forum.ansible.com/u/nbmprivat)\
**Post date:** [October 4, 2018, 8:44am UTC](https://forum.ansible.com/t/loop-with-register-and-query-output/28717/1 "2018-10-04T08:44:01Z")

</div>

Hi there,

I am trying to retrieve azure resources that are unknown.

`

- name: Get CAN resource group facts by tags  
azure\_rm\_resourcegroup\_facts:  
subscription\_id: “{{az\_customer\_subscription\_id}}”  
ad\_user: “{{ad\_user\_login\_var}}”  
password: “{{ad\_password\_var}}”  
tags:

- type:CAN  
register: resource\_groups

- name: Set pca resource group name  
set\_fact:  
rg: “{{ resource\_groups.ansible\_facts.azure\_resourcegroups | map(attribute=‘name’) | list }}” #Dynamic resource group list

`

`

# Register output from virtual machine inside unknown number of resource groups

- name: Get VM info  
azure\_rm\_resource\_facts:  
subscription\_id: “{{az\_customer\_subscription\_id}}”  
ad\_user: “{{ad\_user\_login\_var}}”  
password: “{{ad\_password\_var}}”  
resource\_group: “{{ item }}”  
provider: compute  
resource\_type: virtualMachines  
api\_version: “2017-12-01”  
register: azure\_virtualMachines  
loop: “{{ rg }}”

`

Now the problem is that the variable azure\_virtualMachines contains a list that I don’t know how to query.

`

ok: [localhost] =\> {  
“azure\_virtualMachines”: {  
“changed”: false,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_label”: “rgname2”,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“ad\_user”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“adfs\_authority\_url”: null,  
“api\_profile”: “latest”,  
“api\_version”: “2017-12-01”,  
“auth\_source”: null,  
“cert\_validation\_mode”: null,  
“client\_id”: null,  
“cloud\_environment”: “AzureCloud”,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“profile”: null,  
“provider”: “compute”,  
“resource\_group”: “rgname1”,  
“resource\_name”: null,  
“resource\_type”: “virtualMachines”,  
“secret”: null,  
“subresource”: ,  
“subscription\_id”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“tenant”: null,  
“url”: null  
}  
},  
“item”: “rgname1”,  
“response”: [  
{  
“value”: [  
{  
“id”: “/subscriptions/**/resourceGroups/rgname1/providers/Microsoft.Compute/virtualMachines/test-vm",  
“location”: “westeurope”,  
“name”: “test-vm”,  
}  
]  
},  
{  
“\_ansible\_ignore\_errors”: null,  
“\_ansible\_item\_label”: “rgname2”,  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: false,  
“failed”: false,  
“invocation”: {  
“module\_args”: {  
“ad\_user”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“adfs\_authority\_url”: null,  
“api\_profile”: “latest”,  
“api\_version”: “2017-12-01”,  
“auth\_source”: null,  
“cert\_validation\_mode”: null,  
“client\_id”: null,  
“cloud\_environment”: “AzureCloud”,  
“password”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“profile”: null,  
“provider”: “compute”,  
“resource\_group”: “rgname2”,  
“resource\_name”: null,  
“resource\_type”: “virtualMachines”,  
“secret”: null,  
“subresource”: [],  
“subscription\_id”: “VALUE\_SPECIFIED\_IN\_NO\_LOG\_PARAMETER”,  
“tenant”: null,  
“url”: null  
}  
},  
“item”: “rgname2”,  
“response”: [  
{  
“value”: [  
{  
“id”: "/subscriptions/**/resourceGroups/rgname2/providers/Microsoft.Compute/virtualMachines/test2-vm”,  
“location”: “westeurope”,  
“name”: “test2-vm”,  
}  
]  
}  
}

`

How do get the output like this (don’t mind the json format):

`

“rgname1”: [  
{  
“id”: “/subscriptions/\*\*\*\*\*\*\*\*/resourceGroups/rgname1/providers/Microsoft.Compute/virtualMachines/test-vm”,  
“location”: “westeurope”,  
“name”: “test-vm”,  
}  
],

“rgname2”: [  
{  
“id”: “/subscriptions/\*\*\*\*\*\*\*\*/resourceGroups/rgname2/providers/Microsoft.Compute/virtualMachines/test2-vm”,  
“location”: “westeurope”,  
“name”: “test2-vm”,  
}  
]

`
