0

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
  invocation:
    module_args:
      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
  virtualnetworks:
  - address_prefixes:
    - 111.41.240.0/22
    dns_servers:
    - 11.4.0.36
    - 11.4.0.37
    - 174.31.43.92
    - 174.23.43.92
    id: /subscriptions/SUB-ID/resourceGroups/ARG/providers/Microsoft.Network/virtualNetworks/VNT-NAME
    location: australiaeast
    name: VNT-NAME
    provisioning_state: Succeeded
    subnets:
    - address_prefix: 10.4.240.0/24
      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
      service_endpoints:
      - locations:
        - '*'
        service: Microsoft.KeyVault
      - locations:
        - australiaeast
        - australiasoutheast
        service: Microsoft.Storage
    - address_prefix: 10.4.241.0/25
      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
    tags:
      Environment: General-Non-Production
      Owner: BGL IT

This is how my code looks

  - name: Get facts for one virtual network
    azure_rm_virtualnetwork_info:
      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

0

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 .