My requirement is to gather the VNET id from Azure and use that ID as scope for assigning role based access. I will be using azure_rm_virtualnetwork_info and azure_rm_roleassignment for this purpose.

When I try to use azure_rm_virtualnetwork_info, and register the facts in variable, it gets saved as data type "dict". those facts look like this.

I am interested in the VNET ID to use as scope.

ok: [localhost] => changed=false
      ad_user: null
      adfs_authority_url: null
      api_profile: latest
      auth_source: null
      cert_validation_mode: null
      client_id: null
      cloud_environment: AzureCloud
      name: VNT-NAME
      password: null
      profile: NPE
      resource_group: ARG
      secret: null
      subscription_id: null
      tags: null
      tenant: null
  - address_prefixes:
    id: /subscriptions/SUB-ID/resourceGroups/ARG/providers/Microsoft.Network/virtualNetworks/VNT-NAME
    location: australiaeast
    name: VNT-NAME
    provisioning_state: Succeeded
    - address_prefix:
      id: /subscriptions/SUB-ID/resourceGroups/ARG/providers/Microsoft.Network/virtualNetworks/VNT-NAME/subnets/SUB-001
      name: SUB-001
      network_security_group: null
      provisioning_state: Succeeded
      route_table: null
      - locations:
        - '*'
        service: Microsoft.KeyVault
      - locations:
        - australiaeast
        - australiasoutheast
        service: Microsoft.Storage
    - address_prefix:
      id: /subscriptions/SUB-ID/resourceGroups/ARG/providers/Microsoft.Network/virtualNetworks/VNT-NAME/subnets/SUB-001
      name: BGL-NPE-SND-SYD-SUB-APG-INT-Cluster01
      network_security_group: /subscriptions/SUB-ID/resourceGroups/ARG/providers/Microsoft.Network/virtualNetworks/VNT-NAME/subnets/SUB-001
      provisioning_state: Succeeded
      route_table: null
      Environment: General-Non-Production
      Owner: BGL IT

This is how my code looks

  - name: Get facts for one virtual network
      resource_group: "{{ npe_vnet_resource_group }}"
      name: "{{ snd_syd_vnet }}"
      profile: "{{ profilename }}"
    register: snd_mel_vnet_reg

  - debug:
        var: snd_mel_vnet_reg.virtualnetworks.id

But i get error as below

TASK [debug] *****************************************************************************************************************************************************************************************************************************
task path: /home/devans/AZ-vnet-role-assignment.yml:39
ok: [localhost] =>
  snd_mel_vnet_reg.virtualnetworks.id: 'VARIABLE IS NOT DEFINED!: ''list object'' has no attribute ''id'''

Any help appreciated.

1 Answer 1


I had the exact same issue. I solved it by creating a new fact and with a json_query.

In your case you need to add:

- set_fact: 
    myvnetid: "{{ snd_mel_vnet_reg| json_query('virtualnetworks[0].id') }}"

Use the debug to verify the correct output:

- debug: var=myvnetid 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .