Loop with Register and query output

18 views
Skip to first unread message

Nico

unread,
Oct 4, 2018, 4:44:01 AM10/4/18
to Ansible Project
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", 
                             }
                      ]


Reply all
Reply to author
Forward
0 new messages